java poi如何复制word中的table


废话不多说,直接线上代码,具体的操作步骤都有注释。在复制新的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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM