java使用poi对多个word文档进行合成

Posted 奋进的小土豆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java使用poi对多个word文档进行合成相关的知识,希望对你有一定的参考价值。

 1 package com.kwm.referencemark.test;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.OutputStream;
 7 import java.util.ArrayList;
 8 import java.util.HashMap;
 9 import java.util.List;
10 import java.util.Map;
11 
12 import org.apache.poi.openxml4j.opc.OPCPackage;
13 import org.apache.poi.xwpf.usermodel.BreakType;
14 import org.apache.poi.xwpf.usermodel.Document;
15 import org.apache.poi.xwpf.usermodel.XWPFDocument;
16 import org.apache.poi.xwpf.usermodel.XWPFPictureData;
17 import org.apache.xmlbeans.XmlOptions;
18 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
19 /**
20  * 合成word文档工具类
21  * @ClassName: CompoundWordUtil
22  */
23 public class CompoundWordUtil {
24     public static void main (String[] args) throws Exception {
25         File newFile = new File("C:/Users/v-guoxiao/Documents/trademark/TMReferencemark/testfile/Paticulars.docx");
26         List<File> srcfile = new ArrayList<>();
27         
28         File file1 = new File("C:/Users/v-guoxiao/Documents/trademark/TMReferencemark/testfile/test1.docx");
29         File file2 = new File("C:/Users/v-guoxiao/Documents/trademark/TMReferencemark/testfile/test2.docx");
30         srcfile.add(file1);
31         srcfile.add(file2);
32         
33         try {
34             OutputStream dest = new FileOutputStream(newFile);
35             ArrayList<XWPFDocument> documentList = new ArrayList<>();
36             XWPFDocument doc = null;
37             for (int i = 0; i < srcfile.size(); i++) {
38                 FileInputStream in = new FileInputStream(srcfile.get(i).getPath());
39                 OPCPackage open = OPCPackage.open(in);
40                 XWPFDocument document = new XWPFDocument(open);
41                 documentList.add(document);
42             }
43             for (int i = 0; i < documentList.size(); i++) {
44                 doc = documentList.get(0);
45                 if(i == 0){//首页直接分页,不再插入首页文档内容
46                     documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);
47 //                    appendBody(doc,documentList.get(i));
48                 }else if(i == documentList.size()-1){//尾页不再分页,直接插入最后文档内容
49                     appendBody(doc,documentList.get(i));
50                 }else{
51                     documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);
52                     appendBody(doc,documentList.get(i));
53                 }
54             }
55             doc.write(dest);
56             System.out.println("*****合成成功********");
57             Runtime.getRuntime().exec("cmd /c start winword C:/Users/v-guoxiao/Documents/trademark/TMReferencemark/testfile/Paticulars.docx");//直接调用cmd打开合成文档
58         } catch (Exception e) {
59             e.printStackTrace();
60         }
61     }
62 
63     public static void appendBody(XWPFDocument src, XWPFDocument append) throws Exception {
64         CTBody src1Body = src.getDocument().getBody();
65         CTBody src2Body = append.getDocument().getBody();
66 
67         List<XWPFPictureData> allPictures = append.getAllPictures();
68         // 记录图片合并前及合并后的ID
69         Map<String,String> map = new HashMap<String,String>();
70         for (XWPFPictureData picture : allPictures) {
71             String before = append.getRelationId(picture);
72             //将原文档中的图片加入到目标文档中
73             String after = src.addPictureData(picture.getData(), Document.PICTURE_TYPE_PNG);
74             map.put(before, after);
75         }
76         appendBody(src1Body, src2Body,map);
77     }
78 
79     private static void appendBody(CTBody src, CTBody append,Map<String,String> map) throws Exception {
80         XmlOptions optionsOuter = new XmlOptions();
81         optionsOuter.setSaveOuter();
82         String appendString = append.xmlText(optionsOuter);
83 
84         String srcString = src.xmlText();
85         String prefix = srcString.substring(0,srcString.indexOf(">")+1);
86         String mainPart = srcString.substring(srcString.indexOf(">")+1,srcString.lastIndexOf("<"));
87         String sufix = srcString.substring( srcString.lastIndexOf("<") );
88         String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
89         if (map != null && !map.isEmpty()) {
90             //对xml字符串中图片ID进行替换
91             for (Map.Entry<String, String> set : map.entrySet()) {
92                 addPart = addPart.replace(set.getKey(), set.getValue());
93             }
94         }
95         //将两个文档的xml内容进行拼接
96         CTBody makeBody = CTBody.Factory.parse(prefix+mainPart+addPart+sufix);
97         src.set(makeBody);
98     }
99 }

PS:下面为本示例使用的jar包版本

  poi-3.17.jar

  poi-examples-3.17.jar

  poi-excelant-3.17.jar

  poi-ooxml-3.17.jar

  poi-ooxml-schemas-3.17.jar

  poi-scratchpad-3.17.jar

  xmlbeans-2.6.0.jar

  JDK环境为 JDK1.8

另此种方式在合成带有页眉页脚的文档上对不同的实际需求有一定的限制,请自行拓展。

参考引用自:https://www.cnblogs.com/xingmangdieyi110/p/10185752.html

另附使用java使用jacob合成word参考链接:https://blog.csdn.net/u013930562/article/details/90382430

jacob下载官网链接:http://danadler.com/old/jacob/

以上是关于java使用poi对多个word文档进行合成的主要内容,如果未能解决你的问题,请参考以下文章

java poi-tl 根据模板生成word合并多个word文档

如何通过java将多个word文档合成一个wor

如何使用JAVA,POI读写word文档

如何把word文档导入到数据库中——java POI

java利用poi导出word文档

java利用poi导出word文档