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

如何在Java中使用Apache POI向已有.docx文件添加文本

解决Apache POI向已有docx文件追加文本的问题

你的核心问题是当前代码每次都会新建一个空白的XWPFDocument,而非读取已存在的docx文件,因此无法实现内容追加效果。下面是修正后的实现方案:

关键步骤说明

  • 不要直接new XWPFDocument(),而是通过FileInputStream读取目标已存在文档
  • 在读取后的文档对象中,创建新段落并添加内容
  • 最后将修改后的文档写回原文件

修正后的代码

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

public class DocxAppendDemo {
    public static void appendEngineData(String filePath, String engineName, String nuclearCycle, String engineCycle,
                                       String propellant, String reactorFuel, String nozzleType, String repressurisation) throws IOException {
        // 1. 读取已存在的docx文件
        XWPFDocument docx = new XWPFDocument(new FileInputStream(filePath));
        
        // 添加分隔线段落(可选,用于区分不同数据条目)
        XWPFParagraph dividerPar = docx.createParagraph();
        XWPFRun dividerRun = dividerPar.createRun();
        dividerRun.setText("----------------------------------------");
        dividerRun.addBreak();
        
        // 2. 逐条添加带格式的内容
        // 引擎名称
        XWPFParagraph namePar = docx.createParagraph();
        XWPFRun nameRun = namePar.createRun();
        nameRun.setBold(true); // 标签文本加粗,提升可读性
        nameRun.setText("Your Engine is called: ");
        nameRun.setBold(false);
        nameRun.setText(engineName);
        nameRun.addBreak();
        
        // 引擎循环类型
        XWPFParagraph cyclePar = docx.createParagraph();
        XWPFRun cycleRun = cyclePar.createRun();
        cycleRun.setBold(true);
        cycleRun.setText("Your Engine Cycle is: ");
        cycleRun.setBold(false);
        cycleRun.setText(nuclearCycle + " " + engineCycle);
        cycleRun.addBreak();
        
        // 推进剂类型
        XWPFParagraph propPar = docx.createParagraph();
        XWPFRun propRun = propPar.createRun();
        propRun.setBold(true);
        propRun.setText("Your Propellant is: ");
        propRun.setBold(false);
        propRun.setText(propellant);
        propRun.addBreak();
        
        // 反应堆燃料
        XWPFParagraph fuelPar = docx.createParagraph();
        XWPFRun fuelRun = fuelPar.createRun();
        fuelRun.setBold(true);
        fuelRun.setText("Your Reactor's Fission Fuel is: ");
        fuelRun.setBold(false);
        fuelRun.setText(reactorFuel);
        fuelRun.addBreak();
        
        // 喷管类型
        XWPFParagraph nozzlePar = docx.createParagraph();
        XWPFRun nozzleRun = nozzlePar.createRun();
        nozzleRun.setBold(true);
        nozzleRun.setText("Your engine's nozzle type should be: ");
        nozzleRun.setBold(false);
        nozzleRun.setText(nozzleType);
        nozzleRun.addBreak();
        
        // 储罐增压方式
        XWPFParagraph repressPar = docx.createParagraph();
        XWPFRun repressRun = repressPar.createRun();
        repressRun.setBold(true);
        repressRun.setText("Tank repressurisation would be done: ");
        repressRun.setBold(false);
        repressRun.setText(repressurisation);
        repressRun.addBreak();
        
        // 3. 将修改后的文档写回文件
        try (FileOutputStream out = new FileOutputStream(filePath)) {
            docx.write(out);
        }
        docx.close();
    }
    
    // 调用示例
    public static void main(String[] args) throws IOException {
        String filePath = "C:\\Users\\sfsga\\OneDrive\\Desktop\\obf_reg.docx";
        // 替换为你的随机生成数据即可
        appendEngineData(filePath, "TestEngine", "Nuclear", "Open Cycle", "Liquid Hydrogen", "Uranium-235", "Bell Nozzle", "via Helium bleed");
    }
}

注意事项

  1. 依赖准备:确保项目引入Apache POI相关依赖,Maven依赖示例:
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.5</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.5</version>
</dependency>
  1. 路径格式:Windows系统下文件路径需使用双反斜杠\\,或直接使用正斜杠/
  2. 异常处理:代码中抛出IOException,实际项目建议添加更完善的异常捕获逻辑
  3. 格式扩展:可根据需求调整字体、字号、缩进等,比如用run.setFontSize(12)设置字号

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 11:18:43