SpringBoot+EasyPOI輕松實(shí)現(xiàn)Excel和Word導(dǎo)出PDF
在企業(yè)級(jí)開發(fā)中,將 Excel 和 Word 文檔導(dǎo)出為 PDF 是常見需求(如報(bào)表歸檔、合同存檔)。本文將結(jié)合 ??EasyPOI(Excel 快速導(dǎo)出)?? 和 ??Aspose 系列工具(格式完美轉(zhuǎn)換)??,詳細(xì)講解如何實(shí)現(xiàn) Excel 和 Word 到 PDF 的轉(zhuǎn)換,并解決格式保留、性能優(yōu)化等核心問題。
一、環(huán)境準(zhǔn)備與依賴配置
1.1 方案選型
| 步驟 | 工具/庫 | 特點(diǎn) | 適用場景 |
|---|---|---|---|
| ??Excel 生成?? | EasyPOI(阿里) | 基于 Apache POI 優(yōu)化,支持注解快速生成 Excel,內(nèi)存占用低 | 數(shù)據(jù)量中等(十萬行以內(nèi)) |
| ??Excel 轉(zhuǎn) PDF?? | Aspose.Cells(商業(yè)) | 完美保留 Excel 格式(字體、顏色、合并單元格、圖表等),性能強(qiáng) | 企業(yè)級(jí)高精度轉(zhuǎn)換需求 |
| ??Word 生成?? | Apache POI(XWPF) | 開源,支持 .docx 格式,需手動(dòng)處理復(fù)雜樣式 | 簡單 Word 文檔(文本+表格) |
| ??Word 轉(zhuǎn) PDF?? | Aspose.Words(商業(yè)) | 完美保留 Word 格式(樣式、圖片、頁眉頁腳等),兼容性強(qiáng) | 企業(yè)級(jí)高精度轉(zhuǎn)換需求 |
1.2 依賴配置(商業(yè)庫方案)
??注意??:Aspose 系列工具需購買商業(yè)許可證(試用版有水?。瑴y試階段可使用 免費(fèi)試用版。
在 pom.xml 中添加依賴:
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- EasyPOI(Excel 導(dǎo)出) -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.4.0</version> <!-- 最新穩(wěn)定版 -->
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.4.0</version>
</dependency>
<!-- Aspose.Cells(Excel 轉(zhuǎn) PDF) -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>23.10</version>
</dependency>
<!-- Aspose.Words(Word 轉(zhuǎn) PDF) -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>23.10</version>
</dependency>
</dependencies>
二、Excel 導(dǎo)出 PDF 實(shí)現(xiàn)(EasyPOI + Aspose.Cells)
2.1 定義 Excel 實(shí)體類(EasyPOI 注解)
使用 EasyPOI 的注解簡化 Excel 生成邏輯,支持標(biāo)題、表頭、數(shù)據(jù)校驗(yàn)等:
@Data
@TableName("sys_employee")
// Excel 導(dǎo)出配置(文件名、工作表名)
@ExcelEntity(name = "員工信息表", sheetName = "員工信息")
public class Employee {
@Excel(name = "ID", orderNum = "0", width = 10) // 列名、順序、列寬
private Long id;
@Excel(name = "姓名", orderNum = "1", width = 15)
private String name;
@Excel(name = "年齡", orderNum = "2", width = 10, replace = {"0_未知", "1_男", "2_女"}) // 數(shù)據(jù)替換
private Integer gender;
@Excel(name = "部門", orderNum = "3", width = 20)
private String department;
@Excel(name = "入職時(shí)間", orderNum = "4", width = 20, format = "yyyy-MM-dd") // 日期格式
private Date hireDate;
}
2.2 Excel 生成工具類(EasyPOI)
編寫工具類,封裝 EasyPOI 的 Excel 導(dǎo)出邏輯,生成 Workbook 對(duì)象:
@Component
public class ExcelExportUtils {
private static final Logger log = LoggerFactory.getLogger(ExcelExportUtils.class);
/**
* 生成 Excel Workbook(基于 EasyPOI)
* @param dataList 數(shù)據(jù)列表
* @param entityClass 實(shí)體類(含 @ExcelEntity 注解)
* @return Workbook
*/
public static <T> Workbook generateExcelWorkbook(List<T> dataList, Class<T> entityClass) {
ExportParams exportParams = new ExportParams();
exportParams.setSheetName("員工信息"); // 工作表名(與 @ExcelEntity 一致)
exportParams.settitle("員工信息表"); // Excel 標(biāo)題(頂部大標(biāo)題)
// 使用 EasyPOI 生成 Workbook
return ExcelExportUtil.exportExcel(exportParams, entityClass, dataList);
}
}
2.3 Excel 轉(zhuǎn) PDF 工具類(Aspose.Cells)
使用 Aspose.Cells 加載 Excel 的 Workbook,并轉(zhuǎn)換為 PDF 字節(jié)流,保留所有格式:
@Component
public class ExcelToPdfConverter {
private static final Logger log = LoggerFactory.getLogger(ExcelToPdfConverter.class);
/**
* 將 Workbook 轉(zhuǎn)換為 PDF 字節(jié)流(Aspose.Cells)
* @param workbook Excel Workbook
* @return PDF 字節(jié)流
*/
public static ByteArrayOutputStream convertToPdf(Workbook workbook) throws Exception {
// 1. 創(chuàng)建 PDF 保存選項(xiàng)(可選配置)
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setOnePagePerSheet(true); // 每個(gè)工作表一頁
saveOptions.setFormat(PdfFormat.PdfA2B); // 符合 PDF/A-2B 標(biāo)準(zhǔn)(可選)
saveOptions.setCompressImages(true); // 壓縮圖片(減小文件大?。?
saveOptions.setImageCompression(PdfImageCompressionType.MEDIUM); // 圖片壓縮級(jí)別
// 2. 轉(zhuǎn)換為 PDF 字節(jié)流
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
workbook.save(pdfOutputStream, saveOptions);
return pdfOutputStream;
}
}
2.4 Controller 層整合(Excel 導(dǎo)出 PDF)
編寫接口,接收請(qǐng)求后生成 Excel 并轉(zhuǎn)換為 PDF 下載:
@RestController
@RequestMapping("/export")
public class ExportController {
@Autowired
private ExcelExportUtils excelExportUtils;
@Autowired
private ExcelToPdfConverter pdfConverter;
@GetMapping("/excel-to-pdf")
public void exportEmployeeToPdf(HttpServletResponse response) throws Exception {
// 1. 模擬數(shù)據(jù)(實(shí)際從數(shù)據(jù)庫查詢)
List<Employee> dataList = new ArrayList<>();
dataList.add(new Employee(1L, "張三", 25, "技術(shù)部", new Date()));
dataList.add(new Employee(2L, "李四", 30, "市場部", new Date()));
// 2. 生成 Excel Workbook(EasyPOI)
Workbook workbook = excelExportUtils.generateExcelWorkbook(dataList, Employee.class);
// 3. 轉(zhuǎn)換為 PDF 字節(jié)流(Aspose.Cells)
ByteArrayOutputStream pdfOutputStream = pdfConverter.convertToPdf(workbook);
// 4. 設(shè)置響應(yīng)頭(下載 PDF)
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=employee_report.pdf");
response.getOutputStream().write(pdfOutputStream.toByteArray());
response.getOutputStream().flush();
response.getOutputStream().close();
}
}
三、Word 導(dǎo)出 PDF 實(shí)現(xiàn)(EasyPOI + Aspose.Words)
3.1 定義 Word 模板(Freemarker 或直接生成)
Word 文檔生成通常使用 ??模板引擎??(如 Freemarker),通過替換模板中的占位符生成內(nèi)容。這里以 Aspose.Words 直接生成簡單 Word 為例:
@Component
public class WordDocumentBuilder {
private static final Logger log = LoggerFactory.getLogger(WordDocumentBuilder.class);
/**
* 構(gòu)建 Word 文檔(Aspose.Words)
* @param content 文檔內(nèi)容(文本+表格)
* @return Document(Aspose.Words 文檔對(duì)象)
*/
public static Document buildWordDocument(String content) throws Exception {
// 創(chuàng)建空白文檔
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// 寫入標(biāo)題
builder.getFont().setName("微軟雅黑");
builder.getFont().setSize(16);
builder.getFont().setBold(true);
builder.writeln("員工信息報(bào)告");
// 寫入正文
builder.getFont().setSize(12);
builder.getFont().setBold(false);
builder.writeln("以下是近期員工信息:");
builder.writeln();
// 寫入表格(示例數(shù)據(jù))
builder.startTable();
builder.insertCell();
builder.write("ID");
builder.insertCell();
builder.write("姓名");
builder.insertCell();
builder.write("部門");
builder.endRow();
builder.insertCell();
builder.write("1");
builder.insertCell();
builder.write("張三");
builder.insertCell();
builder.write("技術(shù)部");
builder.endRow();
builder.endTable();
return doc;
}
}
3.2 Word 轉(zhuǎn) PDF 工具類(Aspose.Words)
使用 Aspose.Words 將生成的 Word 文檔轉(zhuǎn)換為 PDF,保留樣式、圖片等:
@Component
public class WordToPdfConverter {
private static final Logger log = LoggerFactory.getLogger(WordToPdfConverter.class);
/**
* 將 Aspose.Words Document 轉(zhuǎn)換為 PDF 字節(jié)流
* @param document Word 文檔對(duì)象
* @return PDF 字節(jié)流
*/
public static ByteArrayOutputStream convertToPdf(Document document) throws Exception {
// 1. 配置 PDF 保存選項(xiàng)(可選)
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setCompressFonts(true); // 壓縮字體
saveOptions.setUseHighQualityRendering(true); // 高質(zhì)量渲染
// 2. 轉(zhuǎn)換為 PDF 字節(jié)流
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
document.save(pdfOutputStream, saveOptions);
return pdfOutputStream;
}
}
3.3 Controller 層整合(Word 導(dǎo)出 PDF)
編寫接口,生成 Word 文檔并轉(zhuǎn)換為 PDF 下載:
@RestController
@RequestMapping("/export")
public class ExportController {
@Autowired
private WordDocumentBuilder wordBuilder;
@Autowired
private WordToPdfConverter wordPdfConverter;
@GetMapping("/word-to-pdf")
public void exportEmployeeToPdf(HttpServletResponse response) throws Exception {
// 1. 構(gòu)建 Word 文檔(Aspose.Words)
String content = "員工信息報(bào)告內(nèi)容..."; // 實(shí)際從數(shù)據(jù)庫或業(yè)務(wù)邏輯獲取
Document wordDoc = wordBuilder.buildWordDocument(content);
// 2. 轉(zhuǎn)換為 PDF 字節(jié)流(Aspose.Words)
ByteArrayOutputStream pdfOutputStream = wordPdfConverter.convertToPdf(wordDoc);
// 3. 設(shè)置響應(yīng)頭(下載 PDF)
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=employee_report.pdf");
response.getOutputStream().write(pdfOutputStream.toByteArray());
response.getOutputStream().flush();
response.getOutputStream().close();
}
}
四、關(guān)鍵問題與優(yōu)化
4.1 格式保留問題
- ??Excel??:Aspose.Cells 支持所有 Excel 特性(合并單元格、條件格式、圖表、公式等),轉(zhuǎn)換后與原文檔一致。
- ??Word??:Aspose.Words 支持樣式(字體、顏色、段落)、圖片、頁眉頁腳、目錄等,復(fù)雜排版(如多欄、文本框)也能完美保留。
4.2 性能優(yōu)化
- ??大數(shù)據(jù)量 Excel??:若數(shù)據(jù)量超過 10 萬行,建議使用 EasyPOI 的
@ExcelEntity配合分頁查詢,避免內(nèi)存溢出。 - ??PDF 壓縮??:通過
PdfSaveOptions配置圖片壓縮(PdfImageCompressionType)和字體嵌入策略,減小文件體積。
4.3 免費(fèi)方案替代(LibreOffice)
若無法使用商業(yè)庫,可通過調(diào)用 LibreOffice 命令行實(shí)現(xiàn)轉(zhuǎn)換(需服務(wù)器安裝 LibreOffice):
// 調(diào)用 LibreOffice 轉(zhuǎn)換 Excel 到 PDF
public static void convertExcelToPdfWithLibreOffice(String excelPath, String pdfPath) throws IOException {
String command = String.format(
"libreoffice --headless --convert-to pdf --outdir %s %s",
pdfPath, excelPath
);
Process process = Runtime.getRuntime().exec(command);
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new RuntimeException("LibreOffice 轉(zhuǎn)換失敗");
}
}
??注意??:此方法格式保留可能不完整(如復(fù)雜樣式丟失),僅適用于小型項(xiàng)目。
五、總結(jié)
本文通過 ??EasyPOI 生成 Excel/Word?? + ??Aspose 系列工具轉(zhuǎn)換 PDF?? 的方案,實(shí)現(xiàn)了 Spring Boot 中 Excel 和 Word 到 PDF 的完整流程。Aspose 工具在格式保留和性能上表現(xiàn)優(yōu)異,適合企業(yè)級(jí)高精度需求;若預(yù)算有限,可嘗試 LibreOffice 命令行方案(需接受格式限制)。實(shí)際項(xiàng)目中需根據(jù)數(shù)據(jù)量和格式要求選擇合適方案。
以上就是SpringBoot+EasyPOI輕松實(shí)現(xiàn)Excel和Word導(dǎo)出PDF的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot Excel和Word導(dǎo)出PDF的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Springboot如何根據(jù)docx填充生成word文件并導(dǎo)出pdf
- SpringBoot動(dòng)態(tài)導(dǎo)出word文檔實(shí)整教程(復(fù)制即可使用)
- SpringBoot使用freemarker導(dǎo)出word文件方法詳解
- springboot利用easypoi實(shí)現(xiàn)簡單導(dǎo)出功能
- SpringBoot+Vue實(shí)現(xiàn)EasyPOI導(dǎo)入導(dǎo)出的方法詳解
- SpringBoot+EasyPoi實(shí)現(xiàn)excel導(dǎo)出功能
- SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出
相關(guān)文章
SpringBoot結(jié)合HTMX實(shí)現(xiàn)高效Web開發(fā)實(shí)戰(zhàn)
在當(dāng)今的 Web 開發(fā)領(lǐng)域,前后端分離已成為主流趨勢,本文將介紹一種輕量級(jí)的解決方案,結(jié)合 Spring Boot 與 HTMX,實(shí)現(xiàn)高效簡潔的前后端分離開發(fā),感興趣的可以了解下2025-07-07
Spring中的@Autowired、@Qualifier和@Primary注解詳解
這篇文章主要介紹了Spring中的@Autowired、@Qualifier和@Primary注解詳解,@Autowired?注解,可以對(duì)類成員變量、方法和構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動(dòng)裝配的工作,@Autowired?是默認(rèn)根據(jù)?byType?進(jìn)行自動(dòng)裝配的,需要的朋友可以參考下2023-11-11
詳解java連接mysql數(shù)據(jù)庫的五種方式
這篇文章主要介紹了詳解java連接mysql數(shù)據(jù)庫的五種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java語言實(shí)現(xiàn)簡單FTP軟件 FTP上傳下載隊(duì)列窗口實(shí)現(xiàn)(7)
這篇文章主要為大家詳細(xì)介紹了Java語言實(shí)現(xiàn)簡單FTP軟件,F(xiàn)TP上傳下載隊(duì)列窗口的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
java中hashmap容量的初始化實(shí)現(xiàn)
這篇文章主要介紹了java中hashmap容量的初始化實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Java實(shí)現(xiàn)圖片與二進(jìn)制的互相轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖片與二進(jìn)制的互相轉(zhuǎn)換,將圖片轉(zhuǎn)二進(jìn)制再將二進(jìn)制轉(zhuǎn)成圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Java OpenCV實(shí)現(xiàn)圖像鏡像翻轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了Java OpenCV實(shí)現(xiàn)圖像鏡像翻轉(zhuǎn)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
23種設(shè)計(jì)模式(12)java模版方法模式
這篇文章主要為大家詳細(xì)介紹了23種設(shè)計(jì)模式之java模版方法模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

