java poi word首行插入文字

添加依赖

复制代码
        <!-- Aspose.Words 依赖 -->
        <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>

代码实现

复制代码
package com.ruoyi.web;

import org.apache.poi.xwpf.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

/**
 * POI 原生实现 Word(docx)正文首行插入文字
 */
public class WordInsertFirstLineByPOI {
    public static void main(String[] args) {
        String inputWordPath = "C:\\Users\\Mayn\\Downloads\\test.docx"; // 输入docx文件路径
        String outputWordPath = "C:\\Users\\Mayn\\Downloads\\out_poi.docx";
        String insertText = "这是在Word正文首行插入的文字 - POI原生实现";

        try (
            FileInputStream fis = new FileInputStream(inputWordPath);
            XWPFDocument doc = new XWPFDocument(fis);
            FileOutputStream fos = new FileOutputStream(outputWordPath)
        ) {
            // 1. 获取文档所有段落
            List<XWPFParagraph> paragraphList = doc.getParagraphs();
            if (paragraphList == null || paragraphList.isEmpty()) {
                // 若文档为空,直接创建新段落插入文字
                XWPFParagraph para = doc.createParagraph();
                XWPFRun run = para.createRun();
                setRunStyle(run, insertText);
            } else {
                // 2. 获取第一个段落(正文首段)
                XWPFParagraph firstPara = paragraphList.get(0);
                // 3. 创建新的Run(文字片段),插入到第一个段落的最前面
                XWPFRun firstRun = firstPara.createRun();
                // 关键:先写入要插入的文字(会自动放在段落开头)
                setRunStyle(firstRun, insertText);
                // 若需要插入后加空格分隔原有文字,可添加:firstRun.setText(" ", 1);
            }

            // 4. 保存文档
            doc.write(fos);
            System.out.println("首行文字插入成功!输出文档路径:" + outputWordPath);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("插入失败:" + e.getMessage());
        }
    }

    /**
     * 自定义POI文字样式
     */
    private static void setRunStyle(XWPFRun run, String text) {
        run.setText(text); // 设置文字内容
        run.setFontFamily("宋体"); // 字体
        run.setFontSize(14); // 字体大小
        run.setBold(true); // 加粗
        // run.setColor("000000"); // 字体颜色(16进制RGB)
    }
}
相关推荐
EXI-小洲9 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
2601_9619017010 小时前
SpringBoot使用Nacos进行application.yml配置管理
java·spring boot·spring
何以解忧,唯有..11 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
QQ_216962909612 小时前
【源码编号:project79475】SpringBoot校内二手交易平台:商品发布、分类检索、留言交流、订单管理全流程实战
java·spring boot·后端
前端 贾公子13 小时前
第09章:上下文与记忆 (2)
java·服务器·前端
2601_9622035113 小时前
Java进阶07集合(续)
java·开发语言
郑州光合科技余经理13 小时前
本地生活服务系统:模块边界与结算字段怎么拆
java·开发语言·前端·后端·系统架构·uni-app·php
roman_日积跬步-终至千里13 小时前
【数据治理(6)】DataOps 不是调度工具,而是让数据产品长期可信的运行机制
java·服务器·网络
落魄大学生之流水线上谋生计14 小时前
LangGraph 基本用法指南
java·windows·python·langchain
2601_9621284114 小时前
SpringBoot篇(缓存层)
java·spring boot·缓存