您的位置:首页 > 健康 > 美食 > 最近最新的新闻_网页界面ps制作步骤_南宁优化推广服务_浏览器下载安装

最近最新的新闻_网页界面ps制作步骤_南宁优化推广服务_浏览器下载安装

2025/4/2 11:43:33 来源:https://blog.csdn.net/qq_36189997/article/details/146396603  浏览:    关键词:最近最新的新闻_网页界面ps制作步骤_南宁优化推广服务_浏览器下载安装
最近最新的新闻_网页界面ps制作步骤_南宁优化推广服务_浏览器下载安装

1,pom类引入依赖

      <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,加载源文档和目标文档

      // 加载源文档和目标文档FileInputStream sourceFile = new FileInputStream(sourcePathStr);FileInputStream targetFile = new FileInputStream(targetPathStr);XWPFDocument sourceDoc = new XWPFDocument(sourceFile);XWPFDocument targetDoc = new XWPFDocument(targetFile);

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

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

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

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

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

                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,移除标志后面的段落

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

后面重新写入文档即可
完成的代码如下

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; // 没有找到匹配的段落}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com