使用NiceXWPFDocument合并word(高效不失真 亲测无误)

在CSDN找了很多方法,合并后的word文档,或多或少存在失真。
经过多种尝试!我终于在poi-tl(poi、poi-tl这两个都是同一个作者写的)的文档中发现了简单好用的方法。
亲测 使用poi-tl的 NiceXWPFDocument 进行word文档合并,文档中的图片、表格、各种统计图(饼状、折线、条形…)、文字,都能很NICE的合并、导出。

  • 需求

    实现两个或多个word文档的合并

  • 代码演示

    • 合并2个word
    //建两个word源对象 NiceXWPFDocument(555我还没有对象)
    NiceXWPFDocument main = new NiceXWPFDocument(new FileInputStream("main.docx"));
    NiceXWPFDocument sub = new NiceXWPFDocument(new FileInputStream("sub.docx"));
    // 合并两个文档
    NiceXWPFDocument newDoc = main.merge(sub);
    // 生成新文档
    FileOutputStream out = new FileOutputStream("new_doc.docx");
    newDoc.write(out);
    newDoc.close();
    out.close();
    
    • 合并多个word
    //新文档的地址
    File dest = new File("D:\\dest.docx");
    FileOutputStream out = new FileOutputStream(dest);
    //之前声明了一个 ArrayList<File> fileList;
    NiceXWPFDocument mainDoc = new NiceXWPFDocument(new FileInputStream(fileList.get(0)));
    for (int i = 1; i < fileList.size(); i++) {
    	NiceXWPFDocument subDoc = new NiceXWPFDocument(new FileInputStream(fileList.get(i)));
    	mainDoc = mainDoc.merge(subDoc);
    }
    mainDoc.write(out);
    mainDoc.close();
    out.close();
    
  • Maven

    <!--poi摸板-->
    <dependency>
        <groupId>com.deepoove</groupId>
        <artifactId>poi-tl</artifactId>
        <version>1.8.1</version>
    </dependency>
    
  • 链接地址

    最后附上poi-tl文档链接:

  1. 点这里:poi-tl:NiceXWPFDocument 合并 word
    或者复制这里:http://deepoove/poi-tl/apache-poi-guide.html#_word%E5%90%88%E5%B9%B6
  2. poi-tl 是一个简化对word操作的工具类,生成各种图表、统计图
    poi-tl的文档
  3. 感谢poi、poi-tl的作者Sayi.

第一次发CSDN!!激动呐!!给我一个赞吧!!

更多推荐

[原创] poi-tl 合并word文档 (使用NiceXWPFDocument)