Java實(shí)現(xiàn)讀取Word模板文檔并替換內(nèi)容生成新文檔
在實(shí)際開(kāi)發(fā)中,經(jīng)常會(huì)遇到需要根據(jù) Word 模板生成特定文檔的需求,比如合同、報(bào)告等。咱們可以使用 Apache POI 庫(kù)來(lái)讀取 Word 模板文檔,然后替換其中的指定內(nèi)容,最后生成新的文檔。下面我就詳細(xì)給大家講講具體怎么做。
1. 引入依賴(lài)
如果你使用的是 Maven 項(xiàng)目,在 pom.xml 中添加以下依賴(lài):
<dependencies> <!-- Apache POI 處理 Word 文檔 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.3</version> </dependency> </dependencies>
2. 創(chuàng)建 Word 模板
首先,創(chuàng)建一個(gè) Word 模板文件 template.docx,在模板中使用特定的占位符來(lái)表示需要替換的內(nèi)容,例如 {name}、{date} 等。假設(shè)模板內(nèi)容如下:
這是一份測(cè)試文檔。
姓名:{name}
日期:{date}
3. Java 代碼實(shí)現(xiàn)
import org.apache.poi.xwpf.usermodel.*; import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; public class WordTemplateProcessor { public static void main(String[] args) { try { // 讀取 Word 模板文件 FileInputStream fis = new FileInputStream("template.docx"); XWPFDocument document = new XWPFDocument(fis); // 準(zhǔn)備要替換的數(shù)據(jù) Map<String, String> data = new HashMap<>(); data.put("{name}", "張三"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); data.put("{date}", sdf.format(new Date())); // 替換文檔中的占位符 replacePlaceholders(document, data); // 保存為新的 Word 文檔 FileOutputStream fos = new FileOutputStream("output.docx"); document.write(fos); fos.close(); fis.close(); System.out.println("新的 Word 文檔生成成功!"); } catch (IOException e) { e.printStackTrace(); System.out.println("生成新的 Word 文檔失?。? + e.getMessage()); } } private static void replacePlaceholders(XWPFDocument document, Map<String, String> data) { // 遍歷文檔中的每個(gè)段落 for (XWPFParagraph paragraph : document.getParagraphs()) { // 遍歷段落中的每個(gè)文本運(yùn)行對(duì)象 for (XWPFRun run : paragraph.getRuns()) { String text = run.getText(0); if (text != null) { // 遍歷數(shù)據(jù)映射,替換占位符 for (Map.Entry<String, String> entry : data.entrySet()) { String placeholder = entry.getKey(); String replacement = entry.getValue(); if (text.contains(placeholder)) { text = text.replace(placeholder, replacement); run.setText(text, 0); } } } } } } }
4. 代碼解釋
讀取 Word 模板文件
FileInputStream fis = new FileInputStream("template.docx"); XWPFDocument document = new XWPFDocument(fis);
通過(guò) FileInputStream 讀取 template.docx 文件,然后使用 XWPFDocument 類(lèi)將其加載到內(nèi)存中。
準(zhǔn)備要替換的數(shù)據(jù)
Map<String, String> data = new HashMap<>(); data.put("{name}", "張三"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); data.put("{date}", sdf.format(new Date()));
創(chuàng)建一個(gè) Map 對(duì)象,將占位符和要替換的內(nèi)容進(jìn)行映射。
替換文檔中的占位符
private static void replacePlaceholders(XWPFDocument document, Map<String, String> data) { for (XWPFParagraph paragraph : document.getParagraphs()) { for (XWPFRun run : paragraph.getRuns()) { String text = run.getText(0); if (text != null) { for (Map.Entry<String, String> entry : data.entrySet()) { String placeholder = entry.getKey(); String replacement = entry.getValue(); if (text.contains(placeholder)) { text = text.replace(placeholder, replacement); run.setText(text, 0); } } } } } }
遍歷文檔中的每個(gè)段落和文本運(yùn)行對(duì)象,檢查文本中是否包含占位符,如果包含則進(jìn)行替換。
保存為新的 Word 文檔
FileOutputStream fos = new FileOutputStream("output.docx"); document.write(fos); fos.close(); fis.close();
使用 FileOutputStream 將替換后的文檔保存為 output.docx 文件。
按照上面的步驟,你就可以使用 Java 讀取 Word 模板文檔并替換指定內(nèi)容,生成新的文檔啦。趕緊動(dòng)手試試吧!
到此這篇關(guān)于Java實(shí)現(xiàn)讀取Word模板文檔并替換內(nèi)容生成新文檔的文章就介紹到這了,更多相關(guān)Java讀取Word模板并替換內(nèi)容內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-Plus?Page?分頁(yè)不生效的問(wèn)題解決
分頁(yè)是常見(jiàn)的一種功能,本文主要介紹了MyBatis-Plus?Page分頁(yè)不生效的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07java動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理示例分享
這篇文章主要介紹了java動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理示例,JDK1.3之后,Java提供了動(dòng)態(tài)代理的技術(shù),允許開(kāi)發(fā)者在運(yùn)行期間創(chuàng)建接口的代理實(shí)例,下面我們使用示例學(xué)習(xí)一下2014-03-03springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼
這篇文章主要介紹了springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12java高并發(fā)InterruptedException異常引發(fā)思考
這篇文章主要為大家介紹了java高并發(fā)InterruptedException異常引發(fā)思考,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08JAVA虛擬機(jī)中 -D, -X, -XX ,-server參數(shù)使用
本文主要介紹了JAVA虛擬機(jī)中 -D, -X, -XX ,-server參數(shù)使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03淺談Strut2如何對(duì)請(qǐng)求參數(shù)的封裝
這篇文章主要介紹了淺談Strut2如何對(duì)請(qǐng)求參數(shù)的封裝,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12Springmvc發(fā)送json數(shù)據(jù)轉(zhuǎn)Java對(duì)象接收
這篇文章主要介紹了Springmvc發(fā)送json數(shù)據(jù)轉(zhuǎn)Java對(duì)象接收,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼
本文主要介紹了mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05MyBatis實(shí)現(xiàn)多表聯(lián)查的詳細(xì)代碼
這篇文章主要介紹了MyBatis如何實(shí)現(xiàn)多表聯(lián)查,通過(guò)實(shí)例代碼給大家介紹使用映射配置文件實(shí)現(xiàn)多表聯(lián)查,使用注解的方式實(shí)現(xiàn)多表聯(lián)查,需要的朋友可以參考下2022-08-08springBoot中myBatisPlus的使用步驟及示例代碼
MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在 Spring Boot 項(xiàng)目里使用它能極大提升開(kāi)發(fā)效率,下面為你詳細(xì)介紹在 Spring Boot 中使用 MyBatis-Plus 的步驟以及示例代碼,感興趣的朋友一起看看吧2025-03-03