废话不多说,直接线上代码,具体的操作步骤都有注释。在复制新的table时插入分页,使得每页一个table
InputStream excelFileInputStream = this.getClass().getClassLoader().getResourceAsStream("templates/order.docx");
XWPFDocument document = new XWPFDocument(excelFileInputStream);
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
//首先生成足够多的table
if (orderGroupBySupAndUsers.size() > 1) { //这里是需要设置到table的数组,更具数组长度创建多个table
for (int i = 1; i < orderGroupBySupAndUsers.size(); i++) {
CTTbl ctTbl = CTTbl.Factory.newInstance(); // 创建新的 CTTbl , table
ctTbl.set(document.getTables().get(0).getCTTbl()); // 复制原来的CTTbl
IBody iBody = document.getTables().get(0).getBody();
BeanUtils.copyProperties(document.getTables().get(0).getBody(), iBody);
XWPFTable newTable = new XWPFTable(ctTbl, iBody); // 新增一个table