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_脚本之家

相关推荐
Bruce_Liuxiaowei10 分钟前
使用批处理脚本安全清理Windows系统垃圾
网络·windows·安全·网络安全
一枚小小程序员哈2 小时前
基于asp.net 的在线餐饮订餐系统的设计与实现/基于c#的网上订餐系统/餐厅管理系统
后端·c#·asp.net
好望角雾眠2 小时前
第三阶段数据库-7:sql中函数,运算符,常用关键字
数据库·笔记·sql·学习·sqlserver·c#
海绵宝宝汉堡包7 小时前
c# 项目 文件夹
开发语言·c#
小陈永不服输7 小时前
Windows下RabbitMQ完整安装指南
windows·分布式·rabbitmq
准女婿_8 小时前
优考试局域网系统V6.0.0版
linux·windows·用户运营
一个处女座的程序猿8 小时前
LLMs之Agent:Windows-MCP的简介、安装和使用方法、案例应用之详细攻略
windows·mcp
曹牧9 小时前
C#:窗体间传值
c#
源代码杀手11 小时前
大模型微调训练资源占用查询:Windows 10 查看 NVIDIA 显卡GPU状态教程(替代 Ubuntu 下 watch nvidia-smi)
linux·windows·ubuntu
超级小忍14 小时前
从零开始:JDK 在 Windows、macOS 和 Linux 上的下载、安装与环境变量配置
java·windows·macos