From 0cb02195fc09207108f437cfed8c6bd668118844 Mon Sep 17 00:00:00 2001 From: zhc077 <565291854@qq.com> Date: Mon, 18 Nov 2024 17:25:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E6=A1=A3=E5=90=88=E5=B9=B6=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=2011.18?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jeecg/test/word/WordExportUtil_test.java | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 jeecg-boot/jeecg-module-demo/src/main/test/java/org/jeecg/test/word/WordExportUtil_test.java diff --git a/jeecg-boot/jeecg-module-demo/src/main/test/java/org/jeecg/test/word/WordExportUtil_test.java b/jeecg-boot/jeecg-module-demo/src/main/test/java/org/jeecg/test/word/WordExportUtil_test.java new file mode 100644 index 0000000..a4c58db --- /dev/null +++ b/jeecg-boot/jeecg-module-demo/src/main/test/java/org/jeecg/test/word/WordExportUtil_test.java @@ -0,0 +1,129 @@ +package org.jeecg.test.word; + +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.xwpf.usermodel.BreakType; +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.jeecgframework.poi.word.WordExportUtil; +import org.junit.Test; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.util.*; + +/** + * @Description: TODO + * @Author: Z.H.C + * @CreateTime: 2024-11-18 15:33 + * @Version: 1.0 + */ +public class WordExportUtil_test { + + /** + * 简单导出没有图片和Excel + */ + @Test + public void SimpleWordExport() { + Map map = new HashMap(); + map.put("department", "Easypoi"); + map.put("person", "JueYue"); + map.put("time", new Date()); + map.put("me", "JueYue"); + map.put("date", "2015-01-03"); + try { + //jeecg-boot/jeecg-boot-base-core/src/main/resources/templates/email/work/test1118.docx + XWPFDocument doc = WordExportUtil.exportWord07("src/main/resources/templates/email/work/test1118.docx", map); + FileOutputStream fos = new FileOutputStream("D:/1.docx"); + doc.write(fos); + fos.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void test2() { + File newFile = new File("C:\\Users\\YX\\Desktop\\testword\\合并2.docx"); + List srcfile = new ArrayList<>(); + + File file1 = new File("C:\\Users\\YX\\Desktop\\testword\\1.docx"); + File file2 = new File("C:\\Users\\YX\\Desktop\\testword\\2.docx"); + File file3 = new File("C:\\Users\\YX\\Desktop\\testword\\3.docx"); + srcfile.add(file1); + srcfile.add(file2); + srcfile.add(file3); + try { + OutputStream dest = new FileOutputStream(newFile); + ArrayList documentList = new ArrayList<>(); + XWPFDocument doc = null; + for (int i = 0; i < srcfile.size(); i++) { + FileInputStream in = new FileInputStream(srcfile.get(i).getPath()); + OPCPackage open = OPCPackage.open(in); + XWPFDocument document = new XWPFDocument(open); + documentList.add(document); + } + for (int i = 0; i < documentList.size(); i++) { + doc = documentList.get(0); + if (i == 0) {//首页直接分页,不再插入首页文档内容 + documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE); +// appendBody(doc,documentList.get(i)); + } else if (i == documentList.size() - 1) {//尾页不再分页,直接插入最后文档内容 + appendBody(doc, documentList.get(i)); + } else { + documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE); + appendBody(doc, documentList.get(i)); + } + } + doc.write(dest); + dest.close(); + + System.out.println("*****合成成功********"); + + // Runtime.getRuntime().exec("cmd /c start winword C:/Users/gouwe/合并.docx");//直接调用cmd打开合成文档 + } catch (Exception e) { + e.printStackTrace(); + } + } + + public 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); + } +}