You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何读取两个Docx文件并将内容依次追加写入第三个Docx文件

如何用Java将第二个Docx内容追加到第三个Docx文件

嘿,我来帮你搞定这个需求~首先得提一句,你贴的代码里用了HWPFDocument,这个类是Apache POI用来处理旧版.doc格式的,如果要操作.docx文件,咱们得用XWPFDocument(属于POI-OOXML模块)。下面是完整的实现方案,包含读取两个Docx并把第二个的内容追加到第三个里:

先搞定依赖

如果用Maven管理项目,记得在pom.xml里添加Apache POI的依赖(用最新稳定版就行):

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.5</version>
</dependency>

完整代码实现

这个代码会先把第一个Docx的内容写入第三个文件,然后把第二个Docx的内容(包括段落、表格和基本样式)追加进去:

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

public class DocxAppendUtil {
    public static void main(String[] args) {
        // 替换成你自己的文件路径
        String firstDocPath = "C:/path/to/your/first.docx";
        String secondDocPath = "C:/path/to/your/second.docx";
        String thirdDocPath = "C:/path/to/your/third.docx";

        try (
            // 自动关闭流的try-with-resources语法,省心又安全
            XWPFDocument firstDoc = new XWPFDocument(new FileInputStream(firstDocPath));
            XWPFDocument secondDoc = new XWPFDocument(new FileInputStream(secondDocPath));
            FileOutputStream outputStream = new FileOutputStream(thirdDocPath)
        ) {
            // 先把第一个Docx的内容写入目标文件
            firstDoc.write(outputStream);
            // 把第二个Docx的内容追加进去
            appendDocxContent(firstDoc, secondDoc);
            // 保存最终的完整内容
            firstDoc.write(outputStream);
            
            System.out.println("搞定啦!第三个Docx文件已经生成:" + thirdDocPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 把源Docx的内容(段落+表格)复制到目标Docx里
     */
    private static void appendDocxContent(XWPFDocument targetDoc, XWPFDocument sourceDoc) {
        // 处理段落
        for (XWPFParagraph para : sourceDoc.getParagraphs()) {
            XWPFParagraph newPara = targetDoc.createParagraph();
            // 复制段落样式
            newPara.getCTP().setPPr(para.getCTP().getPPr());
            // 复制每个文本片段的内容和样式
            for (XWPFRun run : para.getRuns()) {
                XWPFRun newRun = newPara.createRun();
                newRun.getCTR().setRPr(run.getCTR().getRPr());
                newRun.setText(run.getText(0));
            }
        }

        // 处理表格(如果原文档有表格的话)
        for (XWPFTable table : sourceDoc.getTables()) {
            XWPFTable newTable = targetDoc.createTable();
            // 复制表格样式
            newTable.getCTTbl().setTblPr(table.getCTTbl().getTblPr());
            // 逐行逐单元格复制内容
            for (XWPFTableRow row : table.getRows()) {
                XWPFTableRow newRow = newTable.createRow();
                for (XWPFTableCell cell : row.getTableCells()) {
                    XWPFTableCell newCell = newRow.getCell(row.getTableCells().indexOf(cell));
                    newCell.getCTTc().setTcPr(cell.getCTTc().getTcPr());
                    // 复制单元格内的段落
                    for (XWPFParagraph para : cell.getParagraphs()) {
                        XWPFParagraph newPara = newCell.addParagraph();
                        newPara.getCTP().setPPr(para.getCTP().getPPr());
                        for (XWPFRun run : para.getRuns()) {
                            XWPFRun newRun = newPara.createRun();
                            newRun.getCTR().setRPr(run.getCTR().getRPr());
                            newRun.setText(run.getText(0));
                        }
                    }
                }
            }
        }
    }
}

按需调整

如果你的需求是直接把第二个Docx追加到一个全新的第三个Docx(不需要第一个Docx的内容),只需要把firstDoc换成空的新文档就行,比如:

XWPFDocument targetDoc = new XWPFDocument(); // 创建空的目标文档

小提示

  • 这个代码会保留原文档的基本样式(比如字体、段落对齐、表格边框等),如果要处理图片、页眉页脚这类复杂元素,得额外加逻辑哦
  • 记得替换代码里的文件路径成你自己的实际路径
  • 确保Apache POI的版本是兼容的,尽量用最新稳定版,避免出现奇怪的bug

内容的提问来源于stack exchange,提问作者shankar ungale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:29:56