需求一个word文件需要出多种模板内容,多word模板组装,同一模板生成多份组装为一个word文件
package cn.afterturn.easypoi.test.word;
import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.xmlbeans.XmlOptions;
import org.junit.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import java.io.FileOutputStream;
import java.util.*;
public class MyWordExportUtilTest {
@Test
public void test() {
HashMap<String, Object> map = new HashMap<>();
map.put("name", "表头数据");
map.put("type", "类型数据");
//组装用户数据
ArrayList<Map<String, Object>> tableData = new ArrayList<>();
HashMap<String, Object> user1 = new HashMap<>();
user1.put("id", "1");
user1.put("name", "张三");
user1.put("duties", "LOL手游");
user1.put("certificateNo", "410411198408263389");
user1.put("licenceIssuingAuthority", "特种设备作业处");
user1.put("badgeNumber", "2023-10至2024-10");
user1.put("type", user1.get("name") + "-特种设备作业人员证书");
ArrayList<ImageEntity> imgs = new ArrayList<>();
//模拟有6张照片
for (int i = 0; i < 6; i++) {
ImageEntity image = new ImageEntity();
image.setHeight(300);
image.setWidth(650);
image.setUrl("imgs/myimg/img.png");
image.setType(ImageEntity.URL);
imgs.add(image);
}
user1.put("imgs", imgs);
tableData.add(user1);
HashMap<String, Object> user2 = new HashMap<>();
user2.put("id", "2");
user2.put("name", "李四");
user2.put("duties", "王者手游");
user2.put("certificateNo", "410411198408263389");
user2.put("licenceIssuingAuthority", "特种设备作业处");
user2.put("badgeNumber", "2023-10至2024-10");
user2.put("type", "国家市场监督管理总局政务服务平台:\r\n姓名:" + user2.get("name"));
imgs = new ArrayList<>();
//模拟有3张照片
for (int i = 0; i < 3; i++) {
ImageEntity image = new ImageEntity();
image.setHeight(300);
image.setWidth(600);
image.setUrl("imgs/myimg/img.png");
image.setType(ImageEntity.URL);
imgs.add(image);
}
user2.put("imgs", imgs);
tableData.add(user2);
HashMap<String, Object> user3 = new HashMap<>();
user3.put("id", "3");
user3.put("name", "王二");
user3.put("duties", "QQ飞车");
user3.put("certificateNo", "410411198408263389");
user3.put("licenceIssuingAuthority", "国家市场监督管理总局");
user3.put("badgeNumber", "2023-10至2024-10");
user3.put("type", "国家市场监督管理总局政务服务平台:\r\n姓名:" + user3.get("name"));
imgs = new ArrayList<>();
//模拟有1张照片
for (int i = 0; i < 1; i++) {
ImageEntity image = new ImageEntity();
image.setHeight(300);
image.setWidth(600);
image.setUrl("imgs/myimg/img.png");
image.setType(ImageEntity.URL);
imgs.add(image);
}
user3.put("imgs", imgs);
tableData.add(user3);
HashMap<String, Object> user4 = new HashMap<>();
user4.put("id", "4");
user4.put("name", "是麻子");
user4.put("duties", "云顶之义");
user4.put("certificateNo", "410411198408263389");
user4.put("licenceIssuingAuthority", "国家市场监督管理总局");
user4.put("badgeNumber", "2023-10至2024-10");
user4.put("type", "国家市场监督管理总局政务服务平台:\r\n姓名:" + user4.get("name"));
imgs = new ArrayList<>();
//模拟有2张照片
for (int i = 0; i < 1; i++) {
ImageEntity image = new ImageEntity();
image.setHeight(300);
image.setWidth(600);
image.setUrl("imgs/myimg/img.png");
image.setType(ImageEntity.URL);
imgs.add(image);
}
user4.put("imgs", imgs);
tableData.add(user4);
map.put("table", tableData);
try {
List<XWPFDocument> wordList = new ArrayList<>();
XWPFDocument doc = WordExportUtil.exportWord07(
"word/特种设备作业人员登记表导出word模板.docx", map);
wordList.add(doc);
for (Map<String, Object> tableImage : tableData) {
XWPFDocument docImg = WordExportUtil.exportWord07(
"word/特种设备作业人员登记表图片导出word模板.docx", tableImage);
wordList.add(docImg);
}
XWPFDocument xwpfDocument = mergeWord(wordList);
FileOutputStream fos = new FileOutputStream("D:/特种设备作业人员登记表导出word.docx");
xwpfDocument.write(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* word文件合并
*
* @param wordList
* @return
* @throws Exception
*/
public static XWPFDocument mergeWord(List<XWPFDocument> wordList) throws Exception {
if (CollectionUtils.isEmpty(wordList)) {
throw new RuntimeException("待合并的word文档list为空");
}
XWPFDocument doc = wordList.get(0);
int size = wordList.size();
if (size > 1) {
doc.createParagraph().setPageBreak(true);
for (int i = 1; i < size; i++) {
// 从第二个word开始合并
XWPFDocument nextPageDoc = wordList.get(i);
// 最后一页不需要设置分页符
if (i != (size - 1)) {
nextPageDoc.createParagraph().setPageBreak(true);
}
appendBody(doc, nextPageDoc);
}
}
return doc;
}
private static void appendBody(XWPFDocument src, XWPFDocument append) throws Exception {
CTBody src1Body = src.getDocument().getBody();
CTBody src2Body = append.getDocument().getBody();
List<XWPFPictureData> allPictures = append.getAllPictures();
// 记录图片合并前及合并后的ID
Map<String, String> map = new HashMap<>();
for (XWPFPictureData picture : allPictures) {
String before = append.getRelationId(picture);
//将原文档中的图片加入到目标文档中
String after = src.addPictureData(picture.getData(), Document.PICTURE_TYPE_PNG);
map.put(before, after);
}
appendBody(src1Body, src2Body, map);
}
private static void appendBody(CTBody src, CTBody append, Map<String, String> map) throws Exception {
XmlOptions optionsOuter = new XmlOptions();
optionsOuter.setSaveOuter();
String appendString = append.xmlText(optionsOuter);
String srcString = src.xmlText();
String prefix = srcString.substring(0, srcString.indexOf(">") + 1);
String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<"));
String sufix = srcString.substring(srcString.lastIndexOf("<"));
String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
if (map != null && !map.isEmpty()) {
//对xml字符串中图片ID进行替换
for (Map.Entry<String, String> set : map.entrySet()) {
addPart = addPart.replace(set.getKey(), set.getValue());
}
}
//将两个文档的xml内容进行拼接
CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + sufix);
src.set(makeBody);
}
}