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

相关推荐
IGP93 小时前
20250606-C#知识:委托和事件
开发语言·c#
Kookoos4 小时前
ABP VNext 与 Neo4j:构建基于图数据库的高效关系查询
数据库·c#·.net·neo4j·abp vnext
南林yan4 小时前
DLL动态库实现文件遍历功能(Windows编程)
windows
Mike_6664 小时前
win10安装WSL2、Ubuntu24.04
windows·ubuntu·wsl2
张鱼小丸子_微辣5 小时前
.Net Framework 4/C# LINQ*
c#
liulun5 小时前
Skia如何绘制几何图形
c++·windows
old_power5 小时前
UCRT 和 MSVC 的区别(Windows 平台上 C/C++ 开发相关)
c语言·c++·windows
扛枪的书生5 小时前
AD 提权-CVE-2022-26923: CertiFried
windows·渗透·kali·提权·域渗透
..活宝..7 小时前
【Emgu CV教程】11.2、Scharr边缘检测
图像处理·计算机视觉·c#·emgu cv·图像分析
yngsqq7 小时前
事件监听 ——CAD C#二次开发
c#