Java POI实现根据word模板导出-合并多个word模版(含有图表、表格、图片)

前言
在 Java 应用程序中,有时候我们需要将数据导出为 Word 文档,并且动态去生产我们想要的文档,以便进行文档的编辑、打印或共享。本文将介绍如何使用 Java 实现导出 Word 文档的方法,实现不同一级标题的内容都由不同模版组成一个动态的word文档。
一、模版介绍
如上图有多个二级标题内容,将其拆分成十五份模板,在选择其中的几份,组成一份新的word文档
二、依赖
<!-- POI-OXML,用于处理Excel和Word文件 (XML文件格式) -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<!-- POI-OXML-Schemas,用于处理复杂功能,如图表等 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<!-- XMLBeans,POI-OXML-Schemas的依赖 -->
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>5.1.1</version>
</dependency>
如果出现word文档word文档被编译乱码,加上下面的依赖
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<!-- 打成jar包名称 -->
<finalName>${
project.name}</finalName>
<mainClass>org.gls.realname.sync.RealInfoSyncApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<!-- 默认就是repackage 意思是包含依赖jar -->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>docx</nonFilteredFileExtension>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</build>
三、代码样例
3.1实现方法
public static void exportWordCopy(List<String> templatePaths, String temDir, String fileName, Map<String, Object> params, List<ChartInfo> ChartInfoList) {
Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式");
if (!temDir.endsWith("/")) {
temDir = temDir + File.separator;
}
File dir = new File(temDir);
if (!dir.exists()) {
dir.mkdirs();
}
try {
List<XWPFDocument> documentList = new ArrayList<>();
for (int i = 0; i < templatePaths.size(); i++) {
//获取word文档所有信息
XWPFDocument document = WordExportUtil.exportWord07(templatePaths.get(i), params);
//获取表格
List<XWPFTable> tables = document.getTables();
//获取word中所有图表对象
List<XWPFChart> chartsList = document.getCharts();
ChartInfo documentInfo = ChartInfoList.get(i);
if (chartsList.size()>0 && documentInfo.getXValues().length>0){
if (Objects.nonNull(documentInfo.getSeriesNameMore())){
changeChart(documentInfo.getChartTitle(),chartsList.get(0) ,documentInfo.getSeriesNameMore(), documentInfo.getBarValues(), documentInfo.getXValues());
}else {
changeChart2(documentInfo.getChartTitle(),chartsList.get(0) , documentInfo.getSeriesName(), documentInfo.getBarValues(), documentInfo.getXValues());
}
}
if (CollectionUtils.isEmpty(documentInfo.getTableList()) && tables.size()>0){
tableArr(tables.get(0),documentInfo.getTableList());
}
documentList.add(document);
}
if (CollectionUtils.isEmpty(documentList)) {
throw new RuntimeException("待合并的word文档list为空");
}
XWPFDocument doc = documentList.get(0);
int size = documentList.size();
if (size > 1) {
//doc.createParagraph().setPageBreak(true);
for (int i = 1; i < size; i++) {
// 从第二个word开始合并
XWPFDocument nextPageDoc = documentList.get(i);
// 最后一页不需要设置分页符
// if (i != (size-1)) {
// nextPageDoc.createParagraph().setPageBreak(true);
// }
//复制文本、图片
appendBody(doc, nextPageDoc);
//复制图标
List<XWPFChart> charts1 = nextPageDoc.getCharts();
if (charts1.size() > 0) {
for (XWPFChart xwpfChart : charts1) {
//是否是word中自带图表
// 如果是图表元素
if (xwpfChart instanceof XWPFChart) {
XWPFChart chart = doc.createChart(5274310, 3076575);
CTPlotArea plotArea = xwpfChart.getCTChart().getPlotArea();
chart.getCTChart().setPlotArea(plotArea);
chart.getCTChart().setLegend(xwpfChart.getCTChart().getLegend());
chart.setWorkbook(xwpfChart.getWorkbook());
}
}
}
nextPageDoc.removeProtectionEnforcement();
}
}
String tmpPath = temDir + fileName;
FileOutputStream fos = new FileOutputStream(tmpPath);
doc.write(fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//表格 从第一行开始填充数据
private static void changeTable(XWPFTable table, List<String[]> data) {
// 清空表格的原有内容(可选)
int rowIndex = 0;
for (Object[] aData : data) {
XWPFTableRow row = table.getRow(rowIndex);
if (row == null) {
row = table.createRow();
}
if (row.getTableCells().size()