Java poi之word文本替换
生活随笔
收集整理的這篇文章主要介紹了
Java poi之word文本替换
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄結(jié)構(gòu)
- 前言
- 文檔準備
- 引入Maven依賴
- 代碼塊
- 替換結(jié)果驗證
- 孤勇者替換結(jié)果對比
- 青鳥替換結(jié)果對比
前言
應(yīng)公司需求,需實現(xiàn)以下功能
此文章將使用Apache POI實現(xiàn)Word文檔中文本內(nèi)容的替換更新;
Apache POI 是基于 Office Open XML 標準(OOXML)和 Microsoft 的 OLE 2 復(fù)合文檔格式(OLE2)處理各種文件格式的開源項目。 簡而言之,您可以使用 Java 讀寫 MS Excel 文件,可以使用 Java 讀寫 MS Word 和 MS PowerPoint 文件。
- HSSF - 提供讀寫 Microsoft Excel XLS 格式 (Microsoft Excel 97 (-2003)) 檔案的功能。
- XSSF - 提供讀寫 Microsoft Excel OOXML XLSX 格式 (Microsoft Excel XML (2007+)) 檔案的功能。
- SXSSF - 提供低內(nèi)存占用量讀寫 Microsoft Excel OOXML XLSX 格式檔案的功能。
- HWPF - 提供讀寫 Microsoft Word DOC97 格式 (Microsoft Word 97 (-2003)) 檔案的功能。
- XWPF - 提供讀寫 Microsoft Word DOC2003 格式 (WordprocessingML (2007+)) 檔案的功能。
- HSLF/XSLF - 提供讀寫 Microsoft PowerPoint 格式檔案的功能。
- HDGF/XDGF - 提供讀 Microsoft Visio 格式檔案的功能。
- HPBF - 提供讀 Microsoft Publisher 格式檔案的功能。
- HSMF - 提供讀 Microsoft Outlook 格式檔案的功能。
文檔準備
小編準備了以下兩個文檔:《孤勇者.doc》《青鳥.docx》,分別代表不同版本的文檔,里邊分別記錄了各自的歌詞,挑選其中個別詞語進行替換測試,測試目標已用紅藍顏色標注,以便驗證替換結(jié)果,如下圖
引入Maven依賴
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version> </dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>4.1.2</version> </dependency>代碼塊
package com.bjzaxk.utils;import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.ooxml.POIXMLDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun;import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map;public class Demo {public static void main(String[] args) { // String filePath = "C:\\Users\\Administrator\\Desktop\\java_poi\\demo_file\\孤勇者.doc"; // String formart = "DOC";String filePath = "C:\\Users\\Administrator\\Desktop\\java_poi\\demo_file\\青鳥.docx";String formart = "DOCX";Map<String, String> textMap = new HashMap<>();textMap.put("蔚藍的", "湛藍的");textMap.put("振翅高飛", "翱翔天際"); // textMap.put("他們說", "They sey"); // textMap.put("愛你", "Love you");wordTextSubstitution(filePath, formart, textMap);}/*** @param filePath 替換文件所在路徑* @param formart 替換文件擴展名* @param map 替換數(shù)據(jù)集合* @description: 替換word中的文字* @author: Mr.Jkx* @time: 2023/1/10 13:19*/public static void wordTextSubstitution(String filePath, String formart, Map<String, String> map) {String textPath = "";File file = new File(filePath);String fileName = file.getName();try {if ("DOCX".equals(formart)) {if (fileName != null && fileName != "") {String name = fileName.substring(0, fileName.length() - 5);textPath = filePath.replaceAll(fileName, name + "_" + System.currentTimeMillis() + ".docx");}XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(filePath));// 替換段落中的指定文字Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();while (itPara.hasNext()) {XWPFParagraph paragraph = itPara.next();List<XWPFRun> runs = paragraph.getRuns();for (int i = 0; i < runs.size(); i++) {String oneparaString = runs.get(i).getText(runs.get(i).getTextPosition());if (oneparaString != null) {for (Map.Entry<String, String> entry : map.entrySet()) {oneparaString = oneparaString.replace(entry.getKey(), entry.getValue());}runs.get(i).setText(oneparaString, 0);}}}// 創(chuàng)建新文件存放新內(nèi)容FileOutputStream outStream = new FileOutputStream(textPath);document.write(outStream);outStream.close();System.out.println("--- SUCCESS!");} else if ("DOC".equals(formart)) {if (fileName != null && fileName != "") {String name = fileName.substring(0, fileName.length() - 4);textPath = filePath.replaceAll(fileName, name + "_" + System.currentTimeMillis() + ".doc");}HWPFDocument document = new HWPFDocument(new FileInputStream(filePath));Range range = document.getRange();for (Map.Entry<String, String> entry : map.entrySet()) {range.replaceText(entry.getKey(), entry.getValue());}// 創(chuàng)建新文件存放新內(nèi)容FileOutputStream outStream = new FileOutputStream(textPath);document.write(outStream);outStream.close();System.out.println("--- SUCCESS!");}} catch (Exception e) {e.printStackTrace();}} }替換結(jié)果驗證
孤勇者替換結(jié)果對比
青鳥替換結(jié)果對比
總結(jié)
以上是生活随笔為你收集整理的Java poi之word文本替换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 西北大学计算机科学与技术学科评估,陕西高
- 下一篇: 服务器进blso设置虚拟,华硕服务器bi