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

相关推荐
周杰伦fans12 分钟前
.NET 轻量级处理 Excel 文件库 - MiniExce
windows·.net·excel
薄荷撞~可乐36 分钟前
C#高并发与并行理解处理
开发语言·c#
sali-tec1 小时前
C# 基于halcon的视觉工作流-章33-矩状测量
开发语言·人工智能·算法·计算机视觉·c#
王维志1 小时前
Unity Embedded Browser文档翻译
unity·c#
FlYFlOWERANDLEAF1 小时前
DevExpress中Word Processing Document API学习记录
学习·c#·word
dyxal1 小时前
使用tree命令导出文件夹/文件的目录树( Windows 和 macOS)
windows·macos·策略模式
棉晗榜2 小时前
C#写字符串到Modbus中
c#·modbus
AnySpaceOne2 小时前
PDF转Word在线转换教程:多种实用方法分享
学习·pdf·word
特立独行的猫a2 小时前
仓颉语言编程入门:第一个 Windows 下的仓颉应用程序
windows·入门·仓颉
Hello.Reader11 小时前
从零到一上手 Protocol Buffers用 C# 打造可演进的通讯录
java·linux·c#