不用额外新建一个空文档,适合需要不断往一个文档中添加其他文档的情况。适合每页格式固定,添加文件新起一页的情况。可进行测试。

package could.com.hanwen.govapi.Test;import org.apache.commons.collections.CollectionUtils;
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.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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class FileCopyUtil {public void aqscMergeDoc(String outPutPath, String mergeWord) {List<File> srcfile = new ArrayList<>();File file1 = new File(outPutPath);File file2 = new File(mergeWord);srcfile.add(file1);srcfile.add(file2);try {ArrayList<XWPFDocument> documentList = new ArrayList<>();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);}XWPFDocument doc = documentList.get(0);if (CollectionUtils.isEmpty(documentList)) {throw  new RuntimeException("待合并的word文档list为空");}int size = documentList.size();if (size > 1) {doc.createParagraph().setPageBreak(true);for (int i = 1; i < size; i++) {// 从第二个word开始合并XWPFDocument nextPageDoc = documentList.get(i);// 最后一页不需要设置分页符if (i != (size-1)) {nextPageDoc.createParagraph().setPageBreak(true);}appendBody(doc, nextPageDoc);}}new File(outPutPath).delete();OutputStream dest = new FileOutputStream(outPutPath);doc.write(dest);} 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<XWPFPictureData> allPictures = append.getAllPictures();// 记录图片合并前及合并后的IDMap<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 rgex = "<[\\s]*?w:sectPr[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?w:sectPr[\\s]*?>";appendString = appendString.replaceAll(rgex, "");String srcString = src.xmlText();String prefix = srcString.substring(0, srcString.indexOf(">"));String mainPart = srcString.substring(srcString.indexOf(">"), srcString.lastIndexOf("<"));String sufix = srcString.substring(srcString.lastIndexOf("<"));String addPart = appendString.substring(appendString.indexOf(">"), 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);}/*public static void main(String[] args) {List<File> srcfile = new ArrayList<>();File file1 = new File("F:\\安全生产责任制度.docx");File file2 = new File("F:\\安全生产责任制度新增.docx");srcfile.add(file1);srcfile.add(file2);try {ArrayList<XWPFDocument> documentList = new ArrayList<>();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);}XWPFDocument doc = documentList.get(0);if (CollectionUtils.isEmpty(documentList)) {throw  new RuntimeException("待合并的word文档list为空");}int size = documentList.size();if (size > 1) {doc.createParagraph().setPageBreak(true);for (int i = 1; i < size; i++) {// 从第二个word开始合并XWPFDocument nextPageDoc = documentList.get(i);// 最后一页不需要设置分页符if (i != (size-1)) {nextPageDoc.createParagraph().setPageBreak(true);}appendBody(doc, nextPageDoc);}}new File("F:\\安全生产责任制度.docx").delete();OutputStream dest = new FileOutputStream("F:\\安全生产责任制度.docx");doc.write(dest);} catch (Exception e) {e.printStackTrace();}}*/
}

如果希望添加的文档直接接在上一文档末尾将srcString替换:

     // 去除分页符String srcString = src.xmlText().replaceAll( "<w:p><w:r><w:br w:type=\"page\"/></w:r></w:p>", "" ).replaceAll( "<w:r><w:br w:type=\"page\"/></w:r>", "" );

参考资料:
1、http://t.zoukankan.com/zeng1994-p-bef8f7ab14486d489db8ee72b9f8ffc0.html

2、合并格式、去掉分页符

多个word文档合并为一个文档 Java实现相关推荐

  1. 如何将多个文本文档合并到一个文档中?

    2019独角兽企业重金招聘Python工程师标准>>> 在日常工作中,我们需要将多个文本文档合并到一个中,接下来将介绍一种批处理的方法. 工具/原料 文本文档 方法/步骤 如图所示, ...

  2. 【Spire.Doc】合并 Word 文档,将多个文档合并为一个

    使用C# 在文档后添加新页合并 private void SaveToFileInsertText(string sourcePath, string insertPath, string saveT ...

  3. 如何将多个word文档的内容合并到一个文档 多html转word

    为了看文本文件的电子书,大家常常要把多个.html文件一个个拷成一个文件,非常不方便.怎样才能象合并文本文件一样来合并html文件呢?我有一个办法. 1.打开Word. 2.插入---文件---按序多 ...

  4. 如何让多个word文档合并成一个

    几千年没发过动态了,今天随便发发证明本人健在吧! 近两天有几个同学问我Word文档合并成一个,我属实有些懵. 首先建立一个空白文档,在[插入]里面找到[对象],然后选择[从文件中提取],之后按照顺序选 ...

  5. C# 实现将多个word文档合并成一个word文档的功能

    https://www.cnblogs.com/zhenzaizai/p/7782748.html 前段时间项目上遇到这么一个需求,需要将多个OCR识别的word文档合并成一个,于是就在网上找了找,自 ...

  6. 把多个txt文档合并为一个txt文档并排序

    当我们得到多个txt文档时,想要把这些文档的内容合并为一个文档并按首列来进行排序,比如按首列数字升序排序,下面介绍操作方法: 步骤一:把多个txt文件放在同一文件夹下,并新建一个txt文档,在里面写下 ...

  7. Computer:成功解决在WPS中合并论文中两个文档(比如将另一个封皮的文档合并在当前文档当中)

    Computer:成功解决在WPS中合并两个文档(比如将另一个封皮的文档合并在当前文档当中) 目录 解决问题 解决方法 解决问题 经常在写论文的时候,需要这样的一个场景,在WPS中合并两个文档,比如将 ...

  8. 多个文本文档合并为一个文件的方法

    如何将多个txt或html文档合并成一个文件? 适用所有文本文档格式,包括 .txt / .html / .htm / .php / .md / .js / .css 等等-- 方法: 将需要合并的文 ...

  9. 如何将多个excel表格合并成一个_如何将多个PDF文档合并为一个

    在PDF应用越来越广泛的办公环境下,掌握PDF文档的操作技巧是非常有必要的. 我们重要的合同文档一般都是PDF格式的,而且合同一般含有非常多的附件,如果把多份附件合并成一份,该如何操作呢? 往下看 ↓ ...

最新文章

  1. H3C RIPv2配置任务
  2. Silverlight学习笔记之页面跳转
  3. 数据库ORA-00600 [15160]处理
  4. 领域驱动设计,让程序员心中有码
  5. jQuery教学-简单好用的视差滚动特效Parallax Effect
  6. The history and design philosophy of Spring
  7. strnpy函数的用法
  8. 异步tcp通信——APM.Core 服务端概述
  9. 670. Maximum Swap 允许交换一个数 求最大值
  10. 【干货】2020年研发运营安全白皮书.pdf(附下载链接)
  11. 如何啃透周志华的《机器学习》西瓜书?
  12. squid代理与缓存(上)
  13. animation 循环_(五)实践出真知——Python 之定时器、线程、动画制作Animation类...
  14. 一款给变量自动取名的工具
  15. 贵港职称计算机考试网,贵港工程师职称等级时间
  16. 郑州大学083500软件工程复试专业课复习
  17. 利用微软Text-To-Speech朗读文本
  18. 串口通信——S5PV210串口通信接口详解
  19. java 锁降级 知乎_锁降级
  20. 笔记 vue3 如何引入第三方字体

热门文章

  1. vue js 中的鼠标事件
  2. html 赛龙舟游戏,亲子游戏《赛龙舟》旱地龙舟
  3. 暑假社会实践总结_Java—王少飞
  4. Spring Cloud | 第二篇:服务消费者(Ribbon)
  5. 蓝牙耳机哪个品牌好一点?音质好的蓝牙耳机推荐!
  6. PHP手机发送验证码实例
  7. java高级面试题2019
  8. qq钱包里可以充公交卡了
  9. 关于SVN(客户端)重命名文件夹问题
  10. 【R语言】多项式逻辑回归logistic