🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
**1. 封装样式** ```java public class WordUtils { /** * 构建样式。 * * @param styles * @param id 样式唯一ID * @param level 几级标题 * @param size 字体大小 * @param color 字体颜色 * @param fontName 字体名称 */ public static void buildStyles(XWPFStyles styles, String id, int level, int size, String color, String fontName) { CTStyle ctStyle = CTStyle.Factory.newInstance(); CTString styleName = CTString.Factory.newInstance(); CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance(); CTOnOff onoffnull = CTOnOff.Factory.newInstance(); CTPPr ppr = CTPPr.Factory.newInstance(); styleName.setVal(id); indentNumber.setVal(BigInteger.valueOf(level)); //样式定义给定级别的标题 ppr.setOutlineLvl(indentNumber); ctStyle.setStyleId(id); ctStyle.setName(styleName); //数字越低在格式栏中越突出 ctStyle.setUiPriority(indentNumber); ctStyle.setUnhideWhenUsed(onoffnull); //样式将显示在格式栏中 ctStyle.setQFormat(onoffnull); ctStyle.setPPr(ppr); XWPFStyle style = new XWPFStyle(ctStyle); CTHpsMeasure measure = CTHpsMeasure.Factory.newInstance(); CTHpsMeasure measure2 = CTHpsMeasure.Factory.newInstance(); measure.setVal(new BigInteger(String.valueOf(size))); measure2.setVal(new BigInteger(String.valueOf(size))); CTFonts fonts = CTFonts.Factory.newInstance(); fonts.setAscii(fontName); CTRPr rpr = CTRPr.Factory.newInstance(); rpr.setRFonts(fonts); rpr.setSz(measure); rpr.setSzCs(measure2); CTColor ctColor = CTColor.Factory.newInstance(); HexBinaryAdapter adapter = new HexBinaryAdapter(); byte[] bytes = adapter.unmarshal(color); ctColor.setVal(bytes); rpr.setColor(ctColor); style.getCTStyle().setRPr(rpr); style.setType(STStyleType.PARAGRAPH); styles.addStyle(style); } } ``` **2. 根据样式id添加样式** ```java public void write02() throws IOException { XWPFDocument docx = new XWPFDocument(); //获取样式XWPFStyles XWPFStyles styles = docx.createStyles(); String id = UUID.randomUUID().toString(); //构建样式 WordUtils.buildStyles(styles, id, 1, 22, "D00000", "仿宋"); XWPFParagraph title = docx.createParagraph(); title.setAlignment(ParagraphAlignment.CENTER); //添加样式 title.setStyle(id); //创建段落文本 XWPFRun titleRun = title.createRun(); //字体颜色,这里设置的颜色会覆盖样式中设置的颜色 //titleRun.setColor("000000"); //字体大小,保持与样式中的字体大小一样 titleRun.setFontSize(22); //字体名称,保持与样式中的字体名称一样 titleRun.setFontFamily("仿宋"); titleRun.setBold(true); titleRun.setText("主标题"); titleRun.addBreak(); String id2 = UUID.randomUUID().toString(); WordUtils.buildStyles(styles, id2, 2, 18, "663333", "楷体"); XWPFParagraph firstGraph = docx.createParagraph(); firstGraph.setAlignment(ParagraphAlignment.LEFT); //添加样式 firstGraph.setStyle(id2); XWPFRun firstRun = firstGraph.createRun(); firstRun.setFontSize(18); firstRun.setFontFamily("楷体"); firstRun.setBold(true); firstRun.setText("1.1 秋天美景"); firstRun.addBreak(); XWPFParagraph secondGraph = docx.createParagraph(); secondGraph.setAlignment(ParagraphAlignment.LEFT); //添加样式 secondGraph.setStyle(id2); XWPFRun secondRun = secondGraph.createRun(); secondRun.setFontSize(18); secondRun.setFontFamily("楷体"); secondRun.setBold(true); secondRun.setText("1.2 冬天美景"); secondRun.addBreak(); OutputStream out = new FileOutputStream("e:/upload/002.docx"); docx.write(out); out.flush(); out.close(); docx.close(); } ``` ![](https://img.kancloud.cn/12/7b/127b77459fab222be8a65412f0c423bf_2095x799.png) <br/> **** 颜色取值参考:https://www.runoob.com/cssref/css-colors.html