在项目中需要根据合同模板导出合同,合同中有个地方需要导出的是还款计划表,而还款计划表是以表格的形式存在的,且根据期数的不同,表格的行会不同,模板中只有两行,一行是表头行,一行是待填充的数据的第一行,因此需要根据数据的实际情况,复制表格的行,然后对表格中填充数据。在网上查阅资料后,找到一篇博客可以解决问题,因项目代码不便展示,这里就将那篇博客中的代码直接copy下来做个记录。根据这个代码去扩展是能满足复制表格行的使用场景的。
Maven依赖
<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>
核心代码
/**
* 复制行
*/
("/copyRow2")
public void copyRow2(HttpServletResponse response) {
String templateFileName = "D:/test/模板.docx";
try {
InputStream inputStream = new FileInputStream(templateFileName);
XWPFDocument document = new XWPFDocument(inputStream);
XWPFTable table = document.getTableArray(0);
XWPFTableRow twoRow = table.getRow(1);
twoRow.getCell(0).setText("第二行");
//创建第三行
XWPFTableRow threeRow = table.insertNewTableRow(2);
//将第二行复制到第三行
createCellsAndCopyStyles(threeRow, twoRow);
threeRow.getCell(0).setText("第三行");//这个是对每个单元格设置内容,实际使用中可以对这一行的任意单元格设置内容。
//保存文件
String fileName = "模板.docx";
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
OutputStream outputStream = response.getOutputStream();
document.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 复制单元格和样式,这是核心代码,直接使用就行了。
*
* @param targetRow 要复制的行
* @param sourceRow 被复制的行
*/
public static void createCellsAndCopyStyles(XWPFTableRow targetRow, XWPFTableRow sourceRow) {
targetRow.getCtRow().setTrPr(sourceRow.getCtRow().getTrPr());
List<XWPFTableCell> tableCells = sourceRow.getTableCells();
if (CollectionUtils.isEmpty(tableCells)) {
return;
}
for (XWPFTableCell sourceCell : tableCells) {
XWPFTableCell newCell = targetRow.addNewTableCell();
newCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());
List sourceParagraphs = sourceCell.getParagraphs();
if (CollUtil.isEmpty(sourceParagraphs)) {
continue;
}
XWPFParagraph sourceParagraph = (XWPFParagraph) sourceParagraphs.get(0);
List targetParagraphs = newCell.getParagraphs();
XWPFParagraph targetParagraph = CollUtil.isEmpty(targetParagraphs) ? newCell.addParagraph() : (XWPFParagraph) targetParagraphs.get(0);
targetParagraph.getCTP().setPPr(sourceParagraph.getCTP().getPPr());
XWPFRun targetRun = targetParagraph.getRuns().isEmpty()
? targetParagraph.createRun() : targetParagraph.getRuns().get(0);
List<XWPFRun> sourceRunList=sourceParagraph.getRuns();
if (CollUtil.isNotEmpty(sourceRunList)) {
XWPFRun sourceRun=sourceRunList.get(0);
//字体名称
targetRun.setFontFamily(sourceRun.getFontFamily());
//字体大小
targetRun.setFontSize(sourceRun.getFontSize());
//字体颜色
targetRun.setColor(sourceRun.getColor());
//字体加粗
targetRun.setBold(sourceRun.isBold());
//字体倾斜
targetRun.setItalic(sourceRun.isItalic());
}
}
}
以上摘自:https://blog.csdn.net/qq_38974638/article/details/116173509