首先导包:
<!-- easypoi -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
<!-- base必须在4.40以上不然,列表合成word时,表格会错乱 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.4.0</version>
</dependency>
<!-- Word 需要使用 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
<optional>true</optional>
</dependency>
代码:
List<XWPFTableCell> tableCells = table.getRow(rowIndex).getTableCells();
for(XWPFTableCell xwpfTableCell:tableCells){
//获取一格里的内容
List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs();
for(XWPFParagraph xwpfParagraph:paragraphs){
List<XWPFRun> run1=xwpfParagraph.getRuns();
for(XWPFRun r:run1){
//当只是读取文件时,以下方法就可以直接设置字体跟大小,但是注意大小只能设置整数
//r.setFontFamily("黑体");//字体
//r.setFontSize(20);//字体大小 只能传整数
//此方法时获取底层CTR对象,利用CTR对象来设置字体大小,可以设置为带小数的大小
// CTRPr ctrPr = r.getCTR().addNewRPr();
//ctrPr.addNewSzCs().setVal(new BigInteger(String.valueOf(30)));//字体效果大小
//ctrPr.addNewSz().setVal(new BigInteger(String.valueOf(30)));//正常字体大小,传入的值是磅数x2
// 此方法就是设置字体效果为模板设置的默认效果,因为集合合成word时,如果不进行设置可能就会不管你模板设置的字体,输出Times New Roman字体
//获取字体样式对象
CTFonts tmpFonts = r.getCTR().getRPr().getRFonts();
CTFonts tmpFonts0 = r.getCTR().getRPr().getRFonts();
//获取字体样式名称
String fontFamily0 = r.getFontFamily();
tmpFonts.setHint(tmpFonts0.getHint());//设置字体样式的提示信息
tmpFonts.setAscii(fontFamily0);//设置西方字符
tmpFonts.setEastAsia(fontFamily0);//设置东亚字符
tmpFonts.setHAnsi(fontFamily0);//设置汉字字符
tmpFonts.setCs(fontFamily0);//设置复合字体样式
tmpFonts.unsetAsciiTheme();//清除字体样式
tmpFonts.unsetEastAsiaTheme();//清除字体样式
tmpFonts.unsetHAnsiTheme();//清除字体样式
}
}
}