springboot完成复制一个word内容包括格式到另外一个word

1,pom类引入依赖

bash 复制代码
      <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>5.2.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>4.1.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.xmlbeans</groupId>
                <artifactId>xmlbeans</artifactId>
                <version>5.1.1</version>
            </dependency>

2,加载源文档和目标文档

bash 复制代码
      // 加载源文档和目标文档
            FileInputStream sourceFile = new FileInputStream(sourcePathStr);
            FileInputStream targetFile = new FileInputStream(targetPathStr);

            XWPFDocument sourceDoc = new XWPFDocument(sourceFile);
            XWPFDocument targetDoc = new XWPFDocument(targetFile);

3,查找目标文档中的特定位置(例如段落文本内容)

bash 复制代码
			targetParagraphText = "查找的内容"; // 目标文档中你想插入到的段落的文本内容
            int insertIndex = findParagraphIndex(targetDoc, targetParagraphText);

4,复制源文档的段落到目标文档

bash 复制代码
                // 复制源文档的段落到目标文档
                for (XWPFParagraph paragraph : paragraphs) {
                    XWPFParagraph newParagraph = targetDoc.createParagraph();
                    newParagraph.getCTP().set(paragraph.getCTP());  // 复制段落的内容
                }

5,将目标文档中的段落按顺序移动,以便在特定位置插入

bash 复制代码
                List<XWPFParagraph> targetParagraphsNew = new ArrayList<>();
                int size = targetParagraphs.size();
                for (int i = insertIndex+1; i < size; i++) {
//                    XWPFParagraph remove = targetParagraphs.remove(insertIndex+d);
                    XWPFParagraph xwpfParagraph = targetParagraphs.get(i);
                    targetParagraphsNew.add(xwpfParagraph);
                }

6,移除标志后面的段落

bash 复制代码
                //移除标志后面的段落
                for (int e = insertIndex; e < size; e++) {
                    boolean b = targetDoc.removeBodyElement(insertIndex);
                }

后面重新写入文档即可

完成的代码如下

bash 复制代码
package com.lz.app.util;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WordContentFormatCopy{

    public static void copyWordContentFormat(String sourcePathStr, String targetPathStr, String outPathStr, String targetParagraphText){
        try {
            // 加载源文档和目标文档
            FileInputStream sourceFile = new FileInputStream(sourcePathStr);
            FileInputStream targetFile = new FileInputStream(targetPathStr);

            XWPFDocument sourceDoc = new XWPFDocument(sourceFile);
            XWPFDocument targetDoc = new XWPFDocument(targetFile);

            // 查找目标文档中的特定位置(例如段落文本内容)
            targetParagraphText = "*****"; // 目标文档中你想插入到的段落的文本内容
            int insertIndex = findParagraphIndex(targetDoc, targetParagraphText);

            if (insertIndex != -1) {
                // 复制源文档中的所有段落到目标文档的特定位置
                List<XWPFParagraph> paragraphs = sourceDoc.getParagraphs();

                // 将目标文档中的段落转为 ArrayList 以便修改
                List<XWPFParagraph> targetParagraphs = new ArrayList<>(targetDoc.getParagraphs());

                // 复制源文档的段落到目标文档
                for (XWPFParagraph paragraph : paragraphs) {
                    XWPFParagraph newParagraph = targetDoc.createParagraph();
                    newParagraph.getCTP().set(paragraph.getCTP());  // 复制段落的内容
                }
                // 将目标文档中的段落按顺序移动,以便在特定位置插入
                List<XWPFParagraph> targetParagraphsNew = new ArrayList<>();
                int size = targetParagraphs.size();
                for (int i = insertIndex+1; i < size; i++) {
//                    XWPFParagraph remove = targetParagraphs.remove(insertIndex+d);
                    XWPFParagraph xwpfParagraph = targetParagraphs.get(i);
                    targetParagraphsNew.add(xwpfParagraph);
                }

                // 将修改后的段落重新设置到目标文档
                for (XWPFParagraph p : targetParagraphsNew) {
                    targetDoc.createParagraph().getCTP().set(p.getCTP());
                }
                //移除标志后面的段落
                for (int e = insertIndex; e < size; e++) {
                    boolean b = targetDoc.removeBodyElement(insertIndex);
                }

                // 重新写入文档
                FileOutputStream out = new FileOutputStream(outPathStr);
                targetDoc.write(out);
                out.close();
            } else {
                System.out.println("未找到指定的目标段落");
            }

            // 关闭文件流
            sourceFile.close();
            targetFile.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 查找目标文档中特定段落的位置
    private static int findParagraphIndex(XWPFDocument doc, String text) {
        List<XWPFParagraph> paragraphs = doc.getParagraphs();
        for (int i = 0; i < paragraphs.size(); i++) {
            if (paragraphs.get(i).getText().contains(text)) {
                return i; // 返回段落索引
            }
        }
        return -1; // 没有找到匹配的段落
    }
}
相关推荐
安然无虞1 小时前
31天Python入门——第10天:深入理解值传递·引用传递以及深浅拷贝问题
开发语言·后端·python·pyqt
餘yuqn1 小时前
activiti 项目运行时找不到数据表 ACT_GE_PROPERTY
服务器·后端·工作流引擎
binnnngo1 小时前
Spring Boot集成PageHelper:轻松实现数据库分页功能
数据库·spring boot·后端·mybatis·pagehelper
咖啡教室2 小时前
DDD四层架构和MVC三层架构的个人理解和学习笔记
后端·mvc·领域驱动设计
uhakadotcom3 小时前
Locust压力测试:轻松评估系统性能
后端·面试·github
一线大码3 小时前
解决不了问题,就解决提出问题的人
后端·程序员·sonarqube
m0_748238423 小时前
SpringBoot集成Elasticsearch 7.x spring-boot-starter-data-elasticsearch 方式
spring boot·elasticsearch·jenkins
计算机程序设计开发3 小时前
计算机网络技术服务管理基于Spring Boot-SSM
网络·数据库·spring boot·毕业设计·计算机毕业设计
小咕聊编程3 小时前
【含文档+PPT+源码】基于SpringBoot+Vue的贫困地区留守儿童关怀系统
vue.js·spring boot·后端
uhakadotcom3 小时前
Airflow 入门指南:轻松管理工作流
后端·面试·github