使用EasyPoi快速導出Word文檔功能的實現步驟
更新時間:2025年09月28日 10:43:37 作者:Bluecook
EasyPoi 是一個基于 Apache POI 的開源 Java 工具庫,旨在簡化 Excel 和 Word 文檔的操作,本文將詳細介紹如何使用 EasyPoi 快速導出 Word 文檔,需要的朋友可以參考下
一、準備工作
1、引入依賴
在 Maven 項目中,添加以下依賴:
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easy-poi-base</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easy-poi-annotation</artifactId>
<version>4.4.0</version>
</dependency>
二、準備好一個word模版文件
- 在項目的
resources目錄下創(chuàng)建一個template/exportTemplate.docx文件作為模板。 - 使用 EasyPoi 提供的占位符 {{}} 來定義動態(tài)內容的位置。例如:
{{title}}
{{content}}
三、編寫導出方法的工具類
package com.example.simple.util;
import cn.afterturn.easypoi.cache.manager.POICacheManager;
import cn.afterturn.easypoi.word.WordExportUtil;
import cn.afterturn.easypoi.word.entity.MyXWPFDocument;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.util.Map;
@Slf4j
public class ExportWordUtil {
/**
* 通用Word文檔導出方法
* @param templateName 模板文件名
* @param params 模板參數
* @param response HTTP響應對象
* @param outputFileName 輸出文件名
* @throws RuntimeException 導出過程中的異常
*/
public static void exportWordDocument(String templateName, Map<String, Object> params,
HttpServletResponse response, String outputFileName) {
MyXWPFDocument doc = null;
try {
// 獲取模板文檔
doc = getXWPFDocument(templateName);
if (doc == null) {
throw new RuntimeException("未能找到模板文件");
}
// 使用模板引擎替換內容
WordExportUtil.exportWord07(doc, params);
// 設置響應頭
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.setHeader("Content-Disposition", "attachment;filename=" + outputFileName);
// 輸出文檔
try (ServletOutputStream outputStream = response.getOutputStream()) {
doc.write(outputStream);
outputStream.flush();
}
} catch (Exception e) {
log.error("導出Word文檔失敗", e);
throw new RuntimeException("導出Word文檔失敗: " + e.getMessage());
} finally {
// 關閉文檔
if (doc != null) {
try {
doc.close();
} catch (Exception e) {
log.error("關閉文檔失敗", e);
}
}
}
}
public static MyXWPFDocument getXWPFDocument(String templateName) throws IOException {
// 從classpath獲取模板
InputStream is = null;
try {
is = POICacheManager.getFile(templateName);
if (is == null) {
throw new FileNotFoundException("模板文件不存在: " + templateName);
}
return new MyXWPFDocument(is);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
log.error("關閉輸入流失敗", e);
}
}
}
}
}
四、在ExportWordServiceImpl層調用導出word工具方法
注意:在spring-boot3中使用時response引入的是import jakarta.servlet.http.HttpServletResponse;
@Override
public void exportWord(Map<String, Object> map, HttpServletResponse response) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("title", "我是一個好標題");
params.put("content", "我是一個很有深度的內容");
ExportWordUtil.exportWordDocument("template/exportTemplate.docx", params, response, "export.docx");
}
五、前端請求接口后word文檔的展示效果

六、總結
通過 EasyPoi,我們可以輕松實現 Word 文檔的導出功能。希望本文能幫助你快速上手并完成相關開發(fā)任務!
到此這篇關于使用EasyPoi快速導出Word文檔功能的實現步驟的文章就介紹到這了,更多相關EasyPoi導出Word文檔內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java Apache poi 對word doc文件進行讀寫操作
這篇文章主要介紹了Apache poi 對word doc文件進行讀寫操作的相關資料,需要的朋友可以參考下2017-01-01

