廢話不多說,直接線上代碼,具體的操作步驟都有注釋。在復制新的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,使用復制好的Cttbl
XWPFParagraph xwpfParagraph = document.createParagraph();//設置分頁
xwpfParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun xwpfRun = xwpfParagraph.createRun();
xwpfRun.setFontSize(14);//設置每頁的title
xwpfRun.setBold(true);
xwpfRun.setText("計划表");
xwpfParagraph.setPageBreak(true);
document.createTable(); // 創建一個空的Table
//設置table值
setOrderExportTable(newTable, orderGroupBySupAndUsers.get(i), group, startDate, endDate, branchName, select, dateFormat);
document.setTable(i, newTable); // 將table設置到word中
}
}
————————————————
版權聲明:本文為CSDN博主「qq_28469355」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_28469355/article/details/88663195