Java实现合并两个word文档内容

 更新时间:2024年11月17日 14:05:44   作者:程序修理员  
这篇文章主要为大家详细介绍了如何使用Java实现合并两个word文档内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

要在Java中实现将两个Word文档的内容合并,可以使用Apache POI库来操作Word文档。

下面2个word合并包括以下内容的合并和处理:

1、段落、标题合并以及样式处理

2、表格合并以及样式处理

3、单元个内容样式处理

4、带word模板占位符内容处理

步骤:

使用Apache POI读取两个Word文档。

将第二个文档的内容追加到第一个文档的末尾。

保存合并后的Word文档。

依赖配置:

首先,需要在项目中添加Apache POI相关的依赖。如果使用Maven,可以在pom.xml文件中加入以下依赖:

  <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-compress</artifactId>
      <version>1.21</version>
  </dependency>
  <!-- Apache POI for Excel -->
  <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>4.1.2</version>
  </dependency>
  <!-- Apache POI for Word -->
  <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml-schemas</artifactId>
      <version>4.1.2</version>
  </dependency>
  <!-- Apache POI dependencies -->
  <dependency>
      <groupId>org.apache.xmlbeans</groupId>
      <artifactId>xmlbeans</artifactId>
      <version>3.1.0</version>
  </dependency>

代码实现:

package com.meritdata.ddc.common.util;


import org.apache.poi.xwpf.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author dongxiajun
 * @since 2024-11-14 15:15
 */
public class WordMerger {
    /**
     * 合并两个Word文档,将第二个文件合并到第一个文件中
     *
     * @param firstFile    第一个文件
     * @param secondStream 上传的文件
     * @param outputFile   输出文件
     * @throws IOException IOException
     */
    public static void mergeWordDocuments(String firstFile, InputStream secondStream, String outputFile) throws IOException {
        // 使用 try-with-resources 自动关闭资源
        try (FileInputStream fis1 = new FileInputStream(firstFile);
             FileOutputStream out = new FileOutputStream(outputFile)) {
            // 加载第一个文档
            XWPFDocument doc1 = new XWPFDocument(fis1);
            // 加载第二个文档
            XWPFDocument doc2 = new XWPFDocument(secondStream);
            // 合并文档
            appendDocument(doc1, doc2);
            // 保存合并后的文档
            doc1.write(out);
        }
    }

    /**
     * 合并两个Word文档,将第二个文件合并到第一个文件中
     *
     * @param firstFile  第一个文件
     * @param secondFile 第二个文件
     * @param outputFile 输出文件
     * @throws IOException IOException
     */
    public static void mergeWordDocuments(String firstFile, String secondFile, String outputFile) throws IOException {
        // 使用 try-with-resources 自动关闭资源
        try (FileInputStream fis1 = new FileInputStream(firstFile);
             FileInputStream fis2 = new FileInputStream(secondFile);
             FileOutputStream out = new FileOutputStream(outputFile)) {
            // 加载第一个文档
            XWPFDocument doc1 = new XWPFDocument(fis1);
            // 加载第二个文档
            XWPFDocument doc2 = new XWPFDocument(fis2);
            // 合并文档
            appendDocument(doc1, doc2);
            // 保存合并后的文档
            doc1.write(out);
        }
    }

    public static void appendDocument(XWPFDocument doc1, XWPFDocument doc2) {
        // 获取第二个文档的所有部分,保持顺序合并
        for (IBodyElement element : doc2.getBodyElements()) {
            // 根据元素的类型进行不同的处理
            if (element instanceof XWPFParagraph) {
                XWPFParagraph paragraph = (XWPFParagraph) element;
                XWPFParagraph newParagraph = doc1.createParagraph();
                copyParagraph(paragraph, newParagraph);
            } else if (element instanceof XWPFTable) {
                XWPFTable table = (XWPFTable) element;
                XWPFTable newTable = doc1.createTable();
                copyTable(table, newTable);
            }
        }
    }

    private static void copyParagraph(XWPFParagraph source, XWPFParagraph target) {
        // 只复制段落的布局属性(例如对齐、缩进、行距等),但不包括段落样式(避免改变标题级别)
        target.getCTP().setPPr(source.getCTP().getPPr());

        // 复制对齐方式、缩进、行间距等样式
        target.setAlignment(source.getAlignment());
        target.setVerticalAlignment(source.getVerticalAlignment());
        target.setIndentationLeft(source.getIndentationLeft());
        target.setIndentationRight(source.getIndentationRight());
        target.setIndentationFirstLine(source.getIndentationFirstLine());
        target.setSpacingBefore(source.getSpacingBefore());
        target.setSpacingAfter(source.getSpacingAfter());
        target.setSpacingBetween(source.getSpacingBetween());

        // 复制段落中的每个run
        for (XWPFRun run : source.getRuns()) {
            XWPFRun newRun = target.createRun();
            newRun.setText(run.text());
            // 复制格式
            newRun.setBold(run.isBold());
            newRun.setItalic(run.isItalic());
            newRun.setUnderline(run.getUnderline());
            newRun.setColor(run.getColor());
            newRun.setFontSize(run.getFontSize());
            newRun.setFontFamily(run.getFontFamily());
        }
        if (source.getStyle() != null) {
            target.setStyle(source.getStyle());
        }
    }

    // 复制源表格到目标表格,包括其行和单元格的样式和内容
    private static void copyTable(XWPFTable source, XWPFTable target) {
        // 删除目标表格中的默认创建的第一行,确保从空状态开始复制
        target.removeRow(0);

        // 遍历源表格的每一行
        for (XWPFTableRow sourceRow : source.getRows()) {
            // 在目标表格中创建新行
            XWPFTableRow newTableRow = target.createRow();
            // 复制源行的样式和内容到目标新行
            copyTableRow(sourceRow, newTableRow);
        }
    }

    // 将源表格行的样式和内容复制到目标表格行
    private static void copyTableRow(XWPFTableRow source, XWPFTableRow target) {
        // 复制行的样式属性
        target.getCtRow().setTrPr(source.getCtRow().getTrPr());

        // 遍历源行的每一个单元格
        for (int i = 0; i < source.getTableCells().size(); i++) {
            XWPFTableCell sourceCell = source.getCell(i);
            XWPFTableCell targetCell;

            // 如果目标行的单元格还不够,则创建新单元格
            if (i < target.getTableCells().size()) {
                targetCell = target.getCell(i);
            } else {
                targetCell = target.addNewTableCell();
            }
            String text = source.getCell(0).getText();
            if (text.contains("{{$fe")) {
                // 复制单元格的样式属性
                targetCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());
                targetCell.setText(sourceCell.getText());
            } else {
                // 复制单元格的样式和内容
                copyTableCell(sourceCell, targetCell);
            }
        }
    }

    // 将源单元格的样式和内容复制到目标单元格
    private static void copyTableCell(XWPFTableCell source, XWPFTableCell target) {
        // 复制单元格的样式属性
        target.getCTTc().setTcPr(source.getCTTc().getTcPr());
        // 清除目标单元格的段落
        target.getParagraphs().clear();
        // 复制每个段落
        for (XWPFParagraph sourceParagraph : source.getParagraphs()) {
            XWPFParagraph targetParagraph = target.addParagraph();
            copyParagraph(sourceParagraph, targetParagraph);
        }
    }

}

说明:

XWPFDocument:用于读取和写入Word文档(.docx格式)。我们首先通过FileInputStream读取Word文档。

mergeDocuments:该方法将第二个Word文档的所有段落复制到第一个文档中。我们获取第二个文档的所有段落,并将其逐个添加到第一个文档。

XWPFParagraph:表示Word文档中的一个段落。每个段落包含多个“运行”(XWPFRun),每个“运行”代表文档中的一部分文本。

XWPFRun:表示段落中的一段文本,可以设置文本的样式(如字体、大小、加粗、斜体等)。

注意:

合并逻辑:本例中只是简单地将第二个文档的所有段落复制到第一个文档。你可以根据需求修改合并的细节(如按页、按段落或按表格等方式进行合并)。

合并样式:此示例中也将复制了文本的样式(如字体、大小、加粗等)。如果需要更复杂的样式保留,可以根据具体的需求进行调整。

处理表格、图片等:如果文档中包含表格或图片,你可能需要额外的逻辑来处理这些内容。

结果:

运行以上代码后,你将得到一个新的Word文档,内容包含了两个原始文档的所有内容。

到此这篇关于Java实现合并两个word文档内容的文章就介绍到这了,更多相关Java合并word内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • IntelliJ Idea常用11款插件(提高开发效率)

    IntelliJ Idea常用11款插件(提高开发效率)

    这篇文章主要介绍了IntelliJ Idea常用11款插件(提高开发效率),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Mac安装maven并配置镜像源和全局变量

    Mac安装maven并配置镜像源和全局变量

    文章指导如何配置Maven环境变量,需修改exportMAVEN_HOME路径为实际路径(如通过brew命令获取),并添加阿里云镜像以验证安装
    2025-09-09
  • SpringBoot 配置文件给实体注入值方式

    SpringBoot 配置文件给实体注入值方式

    这篇文章主要介绍了SpringBoot 配置文件给实体注入值方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Java Stopwatch类,性能与时间计时器案例详解

    Java Stopwatch类,性能与时间计时器案例详解

    这篇文章主要介绍了Java Stopwatch类,性能与时间计时器案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • Java实现分解任意输入数的质因数算法示例

    Java实现分解任意输入数的质因数算法示例

    这篇文章主要介绍了Java实现分解任意输入数的质因数算法,涉及java数学运算相关操作技巧,需要的朋友可以参考下
    2017-10-10
  • Java中HTTP请求的常见错误与排查解决方法

    Java中HTTP请求的常见错误与排查解决方法

    在Java编程语言中,发送HTTP和HTTPS请求是常见的任务,特别是在开发Web服务客户端或进行API交互时,这篇文章主要介绍了Java中HTTP请求的常见错误与排查解决的相关资料,需要的朋友可以参考下
    2025-08-08
  • springboot打包部署到linux服务器的方法

    springboot打包部署到linux服务器的方法

    这篇文章主要介绍了springboot打包部署到linux服务器的方法,通过实例代码相结合的形式给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-06-06
  • Mybatis Plus 代码生成器的实现

    Mybatis Plus 代码生成器的实现

    这篇文章主要介绍了Mybatis Plus 代码生成器的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • Spring中的@PathVariable注解详细解析

    Spring中的@PathVariable注解详细解析

    这篇文章主要介绍了Spring中的@PathVariable注解详细解析,@PathVariable 是 Spring 框架中的一个注解,用于将 URL 中的变量绑定到方法的参数上,它通常用于处理 RESTful 风格的请求,从 URL 中提取参数值,并将其传递给方法进行处理,需要的朋友可以参考下
    2024-01-01
  • spring redis 如何实现模糊查找key

    spring redis 如何实现模糊查找key

    这篇文章主要介绍了spring redis 如何实现模糊查找key的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08

最新评论