1.新增表格,设置宽度和居中
XWPFTable table = document.createTable();
CTTblPr tblPr = table.getCTTbl().addNewTblPr();
tblPr.addNewJc().setVal(STJc.CENTER);
CTTblWidth tableWidth = tblPr.addNewTblW();
tableWidth.setType(STTblWidth.DXA);
tableWidth.setW(BigInteger.valueOf(11072));
2.新增一行,调整宽度
XWPFTableRow row = table.getRow(0);
XWPFTableCell tableCell = row.addNewTableCell();
CTTcPr ctTcPr = tableCell.getCTTc().isSetTcPr() ? tableCell.getCTTc().getTcPr() : tableCell.getCTTc().addNewTcPr();
CTTblWidth ctTblWidth = ctTcPr.addNewTcW();
ctTblWidth.setW(BigInteger.valueOf(100));
ctTblWidth.setType(STTblWidth.DXA);
3.单个表格设置文字大小
XWPFTableCell tableCell = row.getCell(0);
XWPFRun run = tableCell.addParagraph().createRun();
run.setText(String.valueOf(4396));
run.setFontSize(6);
4.自定义标题并应用
//调用方法
setWordText(document.createParagraph(),"标题一",null,"我是表头",true,false,false);
/**
* word文档插入文字
* @param paragraph 文档信息
* @param style 样式,如果不添加样式,输入null即可
* @param size 文字大小,默认大小为空即可,和样式字段为二选一
* @param isCenter 是否居中
* @param isBold 是否加粗
* @param isBreak 是否需要在文字前换行
* @param text 文字内容
*/
public static void setWordText(@NotNull XWPFParagraph paragraph,String style,Integer size,@NotNull String text,@NotNull Boolean isCenter,@NotNull Boolean isBold,@NotNull Boolean isBreak){
XWPFRun textRun = paragraph.createRun();
if(isBreak){
textRun.addBreak();
}
if(isCenter){
paragraph.setAlignment(ParagraphAlignment.CENTER);
}
if(size != null){
textRun.setFontSize(size);
}
if(isBold){
textRun.setBold(true);
}
if(style != null){
paragraph.setStyle(style);
}
textRun.setText(text);
}
//创建表格
InputStream ins = this.getClass().getClassLoader().getResourceAsStream("templates/xxx报告.docx");
BufferedInputStream bis = new BufferedInputStream(ins);
XWPFDocument document = new XWPFDocument(bis);
XWPFStyles styles = document.createStyles();
//下面就是创建自定义标题的方法
createHeadingStyle(document, styles, "标题一", 1, 45, "000000", "宋体");
/**
*
* @param doc
* @param styles
* @param strStyleId 标题id
* @param headingLevel 标题级别
* @param pointSize 字体大小(/2)
* @param hexColor 字体颜色
* @param typefaceName 字体名称(默认宋体)
*/
public static void createHeadingStyle(XWPFDocument doc, XWPFStyles styles, String strStyleId,
int headingLevel, int pointSize, String hexColor, String typefaceName) {
//创建样式
CTStyle ctStyle = CTStyle.Factory.newInstance();
//设置id
ctStyle.setStyleId(strStyleId);
CTString styleName = CTString.Factory.newInstance();
styleName.setVal(strStyleId);
ctStyle.setName(styleName);
CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
indentNumber.setVal(BigInteger.valueOf(headingLevel));
// 数字越低在格式栏中越突出
ctStyle.setUiPriority(indentNumber);
CTOnOff onoffnull = CTOnOff.Factory.newInstance();
ctStyle.setUnhideWhenUsed(onoffnull);
// 样式将显示在“格式”栏中
ctStyle.setQFormat(onoffnull);
// 样式定义给定级别的标题
CTPPr ppr = CTPPr.Factory.newInstance();
ppr.setOutlineLvl(indentNumber);
ctStyle.setPPr(ppr);
XWPFStyle style = new XWPFStyle(ctStyle);
CTHpsMeasure size = CTHpsMeasure.Factory.newInstance();
size.setVal(new BigInteger(String.valueOf(pointSize)));
CTHpsMeasure size2 = CTHpsMeasure.Factory.newInstance();
size2.setVal(new BigInteger(String.valueOf(pointSize)));
CTFonts fonts = CTFonts.Factory.newInstance();
if(typefaceName == null || typefaceName.equals("")) typefaceName = "宋体";
fonts.setAscii(typefaceName); //字体
CTRPr rpr = CTRPr.Factory.newInstance();
rpr.setRFonts(fonts);
rpr.setSz(size);
rpr.setSzCs(size2); //字体大小
CTColor color=CTColor.Factory.newInstance();
color.setVal(hexToBytes(hexColor));
rpr.setColor(color); //字体颜色
style.getCTStyle().setRPr(rpr);
// is a null op if already defined
style.setType(STStyleType.PARAGRAPH);
styles.addStyle(style);
}