相关jar:
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.17</version></dependency><!-- CTPageSz 横向纵向设置 --><dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.3</version></dependency>
//
XWPFDocument xdoc = new XWPFDocument();CTSectPr sectPr = xdoc.getDocument().getBody().addNewSectPr();
CTPageSz pgsz = sectPr.isSetPgSz() ? sectPr.getPgSz() : sectPr.addNewPgSz();
//设置横板
pgsz.setW(BigInteger.valueOf(15840));
pgsz.setH(BigInteger.valueOf(11907));
pgsz.setOrient(STPageOrientation.LANDSCAPE);setTitleText(xdoc,"标题",18,false,true);
setTitleText(xdoc,"一、文件("+considers.size()+"件)",12,true,false);XWPFTable xTable = xdoc.createTable();//创建表格
CTTblPr tblPr =xTable.getCTTbl().addNewTblPr(); //内容居中 无效果
tblPr.addNewJc().setVal(STJc.CENTER);//兼容wps
CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW(); //列宽自动分割tblWidth.setW(new BigInteger("36000"));//总长度tblWidth.setType(STTblWidth.DXA);xTable.getRow(0).setHeight(300);//高度setBodyText(xTable.getRow(0).getCell(0), "序号", null,true,true,2000);
setBodyText(xTable.getRow(0).addNewTableCell(), "名称", null,true,true,10000);
setBodyText(xTable.getRow(0).addNewTableCell(), "类别", null,true,true,2000);
setBodyText(xTable.getRow(0).addNewTableCell(), "议案", null,true,true,4000);
setBodyText(xTable.getRow(0).addNewTableCell(), "", null,true,true,4000);
setBodyText(xTable.getRow(0).addNewTableCell(), "一审", null,true,true,4000);
setBodyText(xTable.getRow(0).addNewTableCell(), "", null,true,true,4000);
setBodyText(xTable.getRow(0).addNewTableCell(), "二审", null,true,true,4000);
setBodyText(xTable.getRow(0).addNewTableCell(), "", null,true,true,2000);XWPFTableRow rowTitle2 = xTable.createRow();//第二行表头
xTable.getRow(0).setHeight(300);
setBodyText(rowTitle2.getCell(0), "序号", null,true,true,2000);
setBodyText(rowTitle2.getCell(1), "名称", null,true,true,10000);
setBodyText(rowTitle2.getCell(2), "类别", null,true,true,2000);
setBodyText(rowTitle2.getCell(3), "单位", null,true,true,4000);
setBodyText(rowTitle2.getCell(4), "时间", null,true,true,4000);
setBodyText(rowTitle2.getCell(5), "承办机构", null,true,true,4000);
setBodyText(rowTitle2.getCell(6), "时间", null,true,true,4000);
setBodyText(rowTitle2.getCell(7), "承办机构", null,true,true,4000);
setBodyText(rowTitle2.getCell(8), "时间", null,true,true,2000);//合并 行列
XWPFTableCell cell = xTable.getRow(0).getCell(0);
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
cell = xTable.getRow(1).getCell(0);
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);cell = xTable.getRow(0).getCell(1);
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
cell = xTable.getRow(1).getCell(1);
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);cell = xTable.getRow(0).getCell(2);
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART); // addNewVMerge 列
cell = xTable.getRow(1).getCell(2);
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);cell = xTable.getRow(0).getCell(3);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); // addNewHMerge 行
cell = xTable.getRow(0).getCell(4);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);cell = xTable.getRow(0).getCell(5);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
cell = xTable.getRow(0).getCell(6);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);cell = xTable.getRow(0).getCell(7);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
cell = xTable.getRow(0).getCell(8);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);//设置合并单元格的大小 兼容wps
//rowNum:合并的单元格所在行号 fromCellNum:合并的起始单元格 toCellNum:合并的结束单元格 9072:列宽总大小(我写死了9072) columnNum:表格总列数
// table.getRow(rowNum).getCell(fromCellNum).getCTTc().addNewTcPr().addNewTcW()
// .setW(BigInteger.valueOf((9072 / columnNum) * (toCellNum - fromCellNum + 1)));xTable.getRow(0).getCell(4).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf((36000 / 9) * (5 - 4 + 1)));// 创建表格内容
for (int i = 0; i < considers.size(); i++) {PlanYearDTO.ConsiderDto data = considers.get(i);XWPFTableRow row = xTable.createRow();row.setHeight(450);setBodyText(row.getCell(0),(i+1)+"",null,false,true,2000);//长度与列头对齐setBodyText(row.getCell(1),data.getTitle(),null,false,true,10000);setBodyText(row.getCell(2),StringUtils.isNotBlank(data.getFormulation())?classMap.get(data.getFormulation()):"",null,false,true,2000);setBodyText(row.getCell(3),data.getRequestGroupName(),null,false,true,4000);setBodyText(row.getCell(4),data.getRequestDate() == null ? "" : String.valueOf(data.getRequestDate()),null,false,true,4000);setBodyText(row.getCell(5),data.getFirstGroupName(),null,false,true,4000);setBodyText(row.getCell(6),data.getFirstDate() == null ? "" : String.valueOf(data.getFirstDate()),null,false,true,4000);setBodyText(row.getCell(7),data.getSecondGroupName(),null,false,true,4000);setBodyText(row.getCell(8),data.getSecondDate() == null ? "" : String.valueOf(data.getSecondDate()),null,false,true,2000);
}//项目二setTitleText(xdoc,"一、提请审议项目("+considers.size()+"件)",12,true,false);
xTable = xdoc.createTable();tblPr =xTable.getCTTbl().addNewTblPr(); //居中 无效果
tblPr.addNewJc().setVal(STJc.CENTER);// 创建表头数据
xTable.getRow(0).setHeight(300);// 创建表头数据
setBodyText(xTable.getRow(0).getCell(0), "序号", null,true,true,2000);
setBodyText(xTable.getRow(0).addNewTableCell(), "名称", null,true,true,10000);
setBodyText(xTable.getRow(0).addNewTableCell(), "类别", null,true,true,2000);
setBodyText(xTable.getRow(0).addNewTableCell(), "单位", null,true,true,12000);// 创建表格内容
for (int i = 0; i < surveys.size(); i++) {PlanYearDTO.ConsiderDto data = surveys.get(i);XWPFTableRow tableTwoRowTwo = xTable.createRow();XWPFTableRow row = xTable.createRow();row.setHeight(450);setBodyText(row.getCell(0),(i+1)+"",null,false,true,2000);setBodyText(row.getCell(1),data.getTitle(),null,false,true,10000);setBodyText(row.getCell(2),StringUtils.isNotBlank(data.getFormulation())?classMap.get(data.getFormulation()):"",null,false,true,2000);setBodyText(row.getCell(3),data.getDraftGroupName(),null,false,true,12000);
}xdoc.write(response.getOutputStream());
xdoc.close();
/*** 设置表格内容*/private void setTitleText(XWPFDocument xdoc, String text, Integer fontSize, boolean isBold,boolean isCenter) {XWPFParagraph xp = xdoc.createParagraph();XWPFRun r1 = xp.createRun();r1.setText(text);r1.setFontFamily("宋体");if(fontSize != null){r1.setFontSize(fontSize);}if(isBold){r1.setBold(true);}if(isCenter){xp.setAlignment(ParagraphAlignment.CENTER);}}
/*** 设置表格内容*/private void setBodyText(XWPFTableCell cell, String text,Integer fontSize, boolean isBold,boolean isCenter,Integer width) {CTTc cttc = cell.getCTTc();//文字居中CTTcPr cellPr = cttc.addNewTcPr();if(width != null){cellPr.addNewTcW().setW(BigInteger.valueOf(width));}if(isCenter){cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);}XWPFRun rIO = cell.addParagraph().createRun();rIO.setFontFamily("宋体");if(fontSize != null){rIO.setFontSize(fontSize);}else{rIO.setFontSize(12);}if(isBold){rIO.setBold(true);}rIO.setText(text);
// cell.setText(text);}
效果演示:
兼容wps问题(未遇到 暂存):
// 列宽自动分割CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW(); //列宽自动分割tblWidth.setW(new BigInteger("36000"));tblWidth.setType(STTblWidth.DXA);//设置合并单元格的大小 兼容wps
//rowNum:合并的单元格所在行号 fromCellNum:合并的起始单元格 toCellNum:合并的结束单元格 9072:列宽总大小(我写死了9072) columnNum:表格总列数
// table.getRow(rowNum).getCell(fromCellNum).getCTTc().addNewTcPr().addNewTcW()
// .setW(BigInteger.valueOf((9072 / columnNum) * (toCellNum - fromCellNum + 1)));xTable.getRow(0).getCell(4).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf((36000 / 9) * (5 - 4 + 1)));
// XWPFRun 方法说明
//默认:宋体(wps)/等线(office2016) 5号 两端对齐 单倍间距 runX.setText("舜发于畎亩之中, 傅说举于版筑之间, 胶鬲举于鱼盐之中, 管夷吾举于士..."); runX.setBold(false);//加粗 runX.setCapitalized(false);//我也不知道这个属性做啥的 //runX.setCharacterSpacing(5);//这个属性报错 runX.setColor("BED4F1");//设置颜色--十六进制 runX.setDoubleStrikethrough(false);//双删除线 runX.setEmbossed(false);//浮雕字体----效果和印记(悬浮阴影)类似 //runX.setFontFamily("宋体");//字体 runX.setFontFamily("华文新魏", FontCharRange.cs);//字体,范围----效果不详 runX.setFontSize(14);//字体大小 runX.setImprinted(false);//印迹(悬浮阴影)---效果和浮雕类似 runX.setItalic(false);//斜体(字体倾斜) //runX.setKerning(1);//字距调整----这个好像没有效果 runX.setShadow(true);//阴影---稍微有点效果(阴影不明显) //runX.setSmallCaps(true);//小型股------效果不清楚 //runX.setStrike(true);//单删除线(废弃) runX.setStrikeThrough(false);//单删除线(新的替换Strike) //runX.setSubscript(VerticalAlign.SUBSCRIPT);//下标(吧当前这个run变成下标)---枚举 //runX.setTextPosition(5);//字体位置(效果不详) //runX.setUnderline(UnderlinePatterns.DASH_LONG);//各种类型的下划线(枚举)