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

相关推荐
hez20101 小时前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
mudtools14 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫18 小时前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools1 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
玩泥巴的1 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
word·二次开发·office·com互操作
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫2 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
路由侠内网穿透2 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
研华嵌入式2 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
未来之窗软件服务2 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟