word合并

手动方法:

Word拆分生成多个文档与合并多个文档,多人协作办公必备技巧_Word联盟

java方法1_Spire.Doc for Java

优点:那是真简单,五行代码搞定。

缺点:付款哦,导包问题独立仓库

Java 合并 Word 文档

XML 复制代码
 <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc</artifactId>
            <version>12.12.21</version>
        </dependency>
java 复制代码
import com.spire.doc.*;

public class Test{
    public static void main(String[] args){
         
        //获取第一个文档的路径
        String filePath1 = "D:\template_01.docx";
         
        //获取第二个文档的路径
        String filePath2 = "D:\template_02.docx";
         
        //加载第一个文档
        Document document = new Document(filePath1);
         
        //使用insertTextFromFile方法将第二个文档的内容插入到第一个文档
        document.insertTextFromFile(filePath2, FileFormat.Docx_2013);
         
        //保存文档
        document.saveToFile("D:\template_sum.docx", FileFormat.Docx_2013);
         
     }
 }

java方法2_POI

优点:导包方便

缺点:代码量比第一个多

参考:Easypoi实现单模板生成多页word文档 - zeng1994 - 博客园 【含图片】

下列代码适用于无图片的word,含图片格式看参考推荐

java 复制代码
/**
 * @author ymyx
 * @version V1.0
 * @date 2024/3/8 13:45
 */
public class Test {

    public static void main(String[] args) throws Exception {

        //获取第一个文档的路径
        String filePath1 = "D:\\template_01.docx";

        //获取第二个文档的路径
        String filePath2 = "D:\\template_02.docx";

        //加载第一个文档
        XWPFDocument d1 = new XWPFDocument (new FileInputStream(filePath1));

        XWPFDocument d2 = new XWPFDocument (new FileInputStream(filePath2));
//        d1.createParagraph().setPageBreak(true);  本想分页符,结果第二页后仍有,去掉暂时没问题
        appendBody(d1, d2);

        //保存文档
        // 3.将合并后的word文档输出到文件
        FileOutputStream fos = new FileOutputStream("D:\\template_sum.docx");

        d1.write(fos);

        fos.close();
    }
    private static void appendBody(XWPFDocument src, XWPFDocument append) throws Exception {

        CTBody src1 = src.getDocument().getBody();

        CTBody append2 = append.getDocument().getBody();

        XmlOptions optionsOuter = new XmlOptions();
        optionsOuter.setSaveOuter();
        String appendString = append2.xmlText(optionsOuter);

        String srcString = src1.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("<"));
        //将两个文档的xml内容进行拼接
        CTBody makeBody = CTBody.Factory.parse(prefix+mainPart+addPart+sufix);

        src1.set(makeBody);

    }
}

效果图:

图1

图2

合并后

未尝试文章方法:

Java实现合并两个word文档内容_java_脚本之家

相关推荐
隔窗听雨眠1 小时前
PostgreSQL单表从Windows迁移到Linux:差异剖析、方案对比与全流程实战
linux·windows·postgresql
Helix2504 小时前
Ansible混合环境自动化:Windows与Linux统一配置管理实战
windows·自动化·ansible·playbook·配置管理·winrm·混合环境
Java开发追求者5 小时前
windows快速安装docker
windows·docker·安装·1024程序员节
2401_885885046 小时前
国际语音C#接口怎么写?.NET环境下实现国际语音验证码批量下发
开发语言·c#·.net
Helix2509 小时前
Windows ETW与KQL实战:终端性能诊断与遥测数据分析指南
windows·性能诊断·kql·etw·azure monitor·遥测数据·tracert
陈葛杰10 小时前
Adobe Premiere Pro2025安装教程
windows·adobe·开源软件·1024程序员节
承渊政道11 小时前
iOS远程控制电脑用什么好?iPhoneiPad远程控制Windows电脑最佳方案2026横评
windows·ios·远程控制·1024程序员节·iphone ipad
易点云徐源11 小时前
PDF 默认打开方式刚修好,过一分钟又变成“未知应用”?使用 ProcMon 成功抓出后台程序
windows·pdf
我是大猴子12 小时前
py how claude work项目分析
运维·服务器·windows·1024程序员节
淡海水13 小时前
07-03-源码-.NET Core GC标记与扫描
unity·c#·.netcore·gc