Java實現(xiàn)讀取Word模板文檔并替換內(nèi)容生成新文檔
在實際開發(fā)中,經(jīng)常會遇到需要根據(jù) Word 模板生成特定文檔的需求,比如合同、報告等。咱們可以使用 Apache POI 庫來讀取 Word 模板文檔,然后替換其中的指定內(nèi)容,最后生成新的文檔。下面我就詳細給大家講講具體怎么做。
1. 引入依賴
如果你使用的是 Maven 項目,在 pom.xml 中添加以下依賴:
<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)建一個 Word 模板文件 template.docx,在模板中使用特定的占位符來表示需要替換的內(nèi)容,例如 {name}、{date} 等。假設(shè)模板內(nèi)容如下:
這是一份測試文檔。
姓名:{name}
日期:{date}
3. Java 代碼實現(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); // 準備要替換的數(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) { // 遍歷文檔中的每個段落 for (XWPFParagraph paragraph : document.getParagraphs()) { // 遍歷段落中的每個文本運行對象 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);
通過 FileInputStream 讀取 template.docx 文件,然后使用 XWPFDocument 類將其加載到內(nèi)存中。
準備要替換的數(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)建一個 Map 對象,將占位符和要替換的內(nèi)容進行映射。
替換文檔中的占位符
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); } } } } } }
遍歷文檔中的每個段落和文本運行對象,檢查文本中是否包含占位符,如果包含則進行替換。
保存為新的 Word 文檔
FileOutputStream fos = new FileOutputStream("output.docx"); document.write(fos); fos.close(); fis.close();
使用 FileOutputStream 將替換后的文檔保存為 output.docx 文件。
按照上面的步驟,你就可以使用 Java 讀取 Word 模板文檔并替換指定內(nèi)容,生成新的文檔啦。趕緊動手試試吧!
到此這篇關(guān)于Java實現(xiàn)讀取Word模板文檔并替換內(nèi)容生成新文檔的文章就介紹到這了,更多相關(guān)Java讀取Word模板并替換內(nèi)容內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java動態(tài)代理和cglib動態(tài)代理示例分享
這篇文章主要介紹了java動態(tài)代理和cglib動態(tài)代理示例,JDK1.3之后,Java提供了動態(tài)代理的技術(shù),允許開發(fā)者在運行期間創(chuàng)建接口的代理實例,下面我們使用示例學習一下2014-03-03springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼
這篇文章主要介紹了springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12java高并發(fā)InterruptedException異常引發(fā)思考
這篇文章主要為大家介紹了java高并發(fā)InterruptedException異常引發(fā)思考,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08JAVA虛擬機中 -D, -X, -XX ,-server參數(shù)使用
本文主要介紹了JAVA虛擬機中 -D, -X, -XX ,-server參數(shù)使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-03-03Springmvc發(fā)送json數(shù)據(jù)轉(zhuǎn)Java對象接收
這篇文章主要介紹了Springmvc發(fā)送json數(shù)據(jù)轉(zhuǎn)Java對象接收,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10MyBatis實現(xiàn)多表聯(lián)查的詳細代碼
這篇文章主要介紹了MyBatis如何實現(xiàn)多表聯(lián)查,通過實例代碼給大家介紹使用映射配置文件實現(xiàn)多表聯(lián)查,使用注解的方式實現(xiàn)多表聯(lián)查,需要的朋友可以參考下2022-08-08springBoot中myBatisPlus的使用步驟及示例代碼
MyBatis-Plus 是一個 MyBatis 的增強工具,在 Spring Boot 項目里使用它能極大提升開發(fā)效率,下面為你詳細介紹在 Spring Boot 中使用 MyBatis-Plus 的步驟以及示例代碼,感興趣的朋友一起看看吧2025-03-03