poi3.13生成表格
原创
©著作权归作者所有:来自51CTO博客作者某科学的南条的原创作品,请联系作者获取转载授权,否则将追究法律责任
表格生成和换页方法
//创建试验设计表格
XWPFTable tableBanBen = document.createTable();
//设置表级别单元格边距
tableBanBen.setCellMargins(13, 5, 353, 5);
//列宽自动分割
CTTblWidth tableBanBenWidth = tableBanBen.getCTTbl().addNewTblPr().addNewTblW();
tableBanBenWidth.setType(STTblWidth.DXA);
tableBanBenWidth.setW(BigInteger.valueOf(9972));
//创建试验设计标题数组
String[] titleBanBen = new String[]{"序号", "版本号", "版本问题", "变更内容", "描述", "审核人"};
for (int i = 0; i < 12; i++) {
if (i == 0) {
XWPFTableRow tableBanBenRowOne = tableBanBen.getRow(i);
XWPFTableCell xwpfTableCell0 = tableBanBenRowOne.getCell(0);
//水平居中
XWPFParagraph xwpfParagraph0 = xwpfTableCell0.addParagraph();
//垂直居中
xwpfParagraph0.setAlignment(ParagraphAlignment.CENTER);
//水平居中
xwpfParagraph0.setVerticalAlignment(TextAlignment.CENTER);
XWPFRun run0 = xwpfParagraph0.createRun();
run0.setText(titleBanBen[0]);
run0.setFontSize(12);
run0.setBold(true);
for (int j = 1; j < 6; j++) {
XWPFTableCell xwpfTableCell1 = tableBanBenRowOne.addNewTableCell();
//水平居中
XWPFParagraph xwpfParagraph1 = xwpfTableCell1.addParagraph();
//垂直居中
xwpfParagraph1.setAlignment(ParagraphAlignment.CENTER);
//水平居中
xwpfParagraph1.setVerticalAlignment(TextAlignment.CENTER);
XWPFRun run1 = xwpfParagraph1.createRun();
run1.setText(titleBanBen[j]);
run1.setFontSize(12);
run1.setBold(true);
run1.setFontFamily("宋体");
}
} else {
XWPFTableRow tableBanBenRowOne = tableBanBen.createRow();
for (int j = 0; j < 6; j++) {
XWPFTableCell xwpfTableCell1 = tableBanBenRowOne.getCell(j);
//水平居中
XWPFParagraph xwpfParagraph1 = xwpfTableCell1.addParagraph();
//垂直居中
xwpfParagraph1.setAlignment(ParagraphAlignment.CENTER);
//水平居中
xwpfParagraph1.setVerticalAlignment(TextAlignment.CENTER);
XWPFRun run1 = xwpfParagraph1.createRun();
run1.setText("");
run1.setFontSize(12);
run1.setBold(false);
run1.setFontFamily("宋体");
}
}
}
//换页
XWPFParagraph page1 = document.createParagraph();
//给这个段落添加一个分隔符即可。
page1.setPageBreak(true);