diff --git a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/WordUtils.java b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/WordUtils.java new file mode 100644 index 0000000..92798a2 --- /dev/null +++ b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/WordUtils.java @@ -0,0 +1,84 @@ +package org.jeecg.common.util; + +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.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody; +import org.springframework.util.CollectionUtils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @Description: TODO + * @Author: Z.H.C + * @CreateTime: 2024-11-22 09:16 + * @Version: 1.0 + */ +public class WordUtils { + + /** + * @description: word文件合并 + * @param: [wordList] + * @return: org.apache.poi.xwpf.usermodel.XWPFDocument + * @author: z.h.c + * @date: 24/11/22 09:19 + */ + public static XWPFDocument mergeWord(List 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 allPictures = append.getAllPictures(); + // 记录图片合并前及合并后的ID + Map 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 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 set : map.entrySet()) { + addPart = addPart.replace(set.getKey(), set.getValue()); + } + } + //将两个文档的xml内容进行拼接 + CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + sufix); + src.set(makeBody); + } +} diff --git a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/service/impl/Project4WordServiceImpl.java b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/service/impl/Project4WordServiceImpl.java index 6abff96..ba2d756 100644 --- a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/service/impl/Project4WordServiceImpl.java +++ b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/service/impl/Project4WordServiceImpl.java @@ -10,6 +10,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.jeecg.common.system.vo.DictModel; import org.jeecg.common.util.DateUtils; +import org.jeecg.common.util.WordUtils; import org.jeecg.modules.demo.departext.dto.DepartExtDto; import org.jeecg.modules.demo.departext.entity.DepartExt; import org.jeecg.modules.demo.departext.service.IDepartExtService; @@ -36,8 +37,10 @@ import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -250,12 +253,25 @@ public class Project4WordServiceImpl extends ServiceImpl System.out.println("map = " + JSON.toJSONString(map)); try { File file = new File(model4wordPath + File.separator); - String modelFilePath = file.getPath() + File.separator + "model2.doc"; - log.info("the model path:{}",modelFilePath); + String basePath = file.getPath() + File.separator; + String modelFilePath = basePath + "baseInfoModel.doc"; + log.info("the baseInfoModel path:{}", modelFilePath); + //1 生成文件 XWPFDocument doc = WordExportUtil.exportWord07(modelFilePath, map); - FileOutputStream fos = new FileOutputStream("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\test1121v13.docx"); + List wordList = new ArrayList<>(); + wordList.add(doc); + FileInputStream fis = new FileInputStream("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\正文V15.docx"); + wordList.add(new XWPFDocument(fis)); + + // 2.把集合里面的word文档全部合并在一个文档 + XWPFDocument mergeWord = WordUtils.mergeWord(wordList); + File outDir = new File(basePath+"out"); + if (!outDir.exists()) { + outDir.mkdirs(); + } + FileOutputStream fos = new FileOutputStream(new File(outDir, "mergework1122.docx")); // FileOutputStream fos2 = new FileOutputStream("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\test1121v7.docx"); - doc.write(fos); + mergeWord.write(fos); fos.close(); } catch (Exception e) { e.printStackTrace(); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-start/src/test/java/org/jeecg/modules/word/WordExportUtil_test.java b/jeecg-boot/jeecg-module-system/jeecg-system-start/src/test/java/org/jeecg/modules/word/WordExportUtil_test.java index 7d191f0..34b88aa 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-start/src/test/java/org/jeecg/modules/word/WordExportUtil_test.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-start/src/test/java/org/jeecg/modules/word/WordExportUtil_test.java @@ -82,15 +82,15 @@ public class WordExportUtil_test { @Test public void test2() { - File newFile = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\合并1121.docx"); + File newFile = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\合并1122v2.docx"); List srcfile = new ArrayList<>(); - File file1 = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\test1121v1.docx"); + File file1 = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\正文V14.docx"); File file2 = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\正文V15.docx"); - File file3 = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\yiJianModel.doc"); +// File file3 = new File("C:\\Users\\YX\\Desktop\\testword\\xiangmushu\\yiJianModel.doc"); srcfile.add(file1); srcfile.add(file2); - srcfile.add(file3); +// srcfile.add(file3); try { OutputStream dest = new FileOutputStream(newFile); ArrayList documentList = new ArrayList<>();