Java實現(xiàn)讀取Word模板文檔并替換內容生成新文檔
在實際開發(fā)中,經(jīng)常會遇到需要根據(jù) Word 模板生成特定文檔的需求,比如合同、報告等。咱們可以使用 Apache POI 庫來讀取 Word 模板文檔,然后替換其中的指定內容,最后生成新的文檔。下面我就詳細給大家講講具體怎么做。
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,在模板中使用特定的占位符來表示需要替換的內容,例如 {name}、{date} 等。假設模板內容如下:
這是一份測試文檔。
姓名:{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 類將其加載到內存中。
準備要替換的數(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 對象,將占位符和要替換的內容進行映射。
替換文檔中的占位符
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 模板文檔并替換指定內容,生成新的文檔啦。趕緊動手試試吧!
到此這篇關于Java實現(xiàn)讀取Word模板文檔并替換內容生成新文檔的文章就介紹到這了,更多相關Java讀取Word模板并替換內容內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用springboot整合websocket實現(xiàn)群聊教程
websocket怎么說呢,就是服務器可以主動向客戶端發(fā)起對話,下面就是springboot整合websocket實現(xiàn)群聊的操作代碼,一起來看一下get新技能吧2021-08-08
springboot使用Scheduling實現(xiàn)動態(tài)增刪啟停定時任務教程
這篇文章主要介紹了springboot使用Scheduling實現(xiàn)動態(tài)增刪啟停定時任務教程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
Java使用EasyExcel實現(xiàn)Excel的導入導出
這篇文章主要給大家介紹了關于Java使用EasyExcel實現(xiàn)Excel的導入導出,在各種系統(tǒng)中,導入導出的數(shù)據(jù)一般都是通過Excel來完成的,需要的朋友可以參考下2023-07-07

