Spring Boot + EasyPOI 实现 Excel 和 Word 导出 PDF 详细教程

Spring Boot + EasyPOI 实现 Excel 和 Word 导出 PDF 详细教程

在企业级开发中,将 Excel 和 Word 文档导出为 PDF 是常见需求(如报表归档、合同存档)。本文将结合 ​​EasyPOI(Excel 快速导出)​ ​ 和 ​​Aspose 系列工具(格式完美转换)​​,详细讲解如何实现 Excel 和 Word 到 PDF 的转换,并解决格式保留、性能优化等核心问题。

一、环境准备与依赖配置

1.1 方案选型

步骤 工具/库 特点 适用场景
​Excel 生成​ EasyPOI(阿里) 基于 Apache POI 优化,支持注解快速生成 Excel,内存占用低 数据量中等(十万行以内)
​Excel 转 PDF​ Aspose.Cells(商业) 完美保留 Excel 格式(字体、颜色、合并单元格、图表等),性能强 企业级高精度转换需求
​Word 生成​ Apache POI(XWPF) 开源,支持 .docx 格式,需手动处理复杂样式 简单 Word 文档(文本+表格)
​Word 转 PDF​ Aspose.Words(商业) 完美保留 Word 格式(样式、图片、页眉页脚等),兼容性强 企业级高精度转换需求

1.2 依赖配置(商业库方案)

​注意​ ​:Aspose 系列工具需购买商业许可证(试用版有水印),测试阶段可使用 免费试用版

pom.xml 中添加依赖:

xml 复制代码
<dependencies>
    <!-- Spring Boot Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- EasyPOI(Excel 导出) -->
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-base</artifactId>
        <version>4.4.0</version> <!-- 最新稳定版 -->
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-web</artifactId>
        <version>4.4.0</version>
    </dependency>
    
    <!-- Aspose.Cells(Excel 转 PDF) -->
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-cells</artifactId>
        <version>23.10</version>
    </dependency>
    
    <!-- Aspose.Words(Word 转 PDF) -->
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-words</artifactId>
        <version>23.10</version>
    </dependency>
</dependencies>

二、Excel 导出 PDF 实现(EasyPOI + Aspose.Cells)

2.1 定义 Excel 实体类(EasyPOI 注解)

使用 EasyPOI 的注解简化 Excel 生成逻辑,支持标题、表头、数据校验等:

less 复制代码
@Data
@TableName("sys_employee")
// Excel 导出配置(文件名、工作表名)
@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_女"})  // 数据替换
    private Integer gender;

    @Excel(name = "部门", orderNum = "3", width = 20)
    private String department;

    @Excel(name = "入职时间", orderNum = "4", width = 20, format = "yyyy-MM-dd")  // 日期格式
    private Date hireDate;
}

2.2 Excel 生成工具类(EasyPOI)

编写工具类,封装 EasyPOI 的 Excel 导出逻辑,生成 Workbook 对象:

java 复制代码
@Component
public class ExcelExportUtils {
    private static final Logger log = LoggerFactory.getLogger(ExcelExportUtils.class);

    /**
     * 生成 Excel Workbook(基于 EasyPOI)
     * @param dataList 数据列表
     * @param entityClass 实体类(含 @ExcelEntity 注解)
     * @return Workbook
     */
    public static <T> Workbook generateExcelWorkbook(List<T> dataList, Class<T> entityClass) {
        ExportParams exportParams = new ExportParams();
        exportParams.setSheetName("员工信息");  // 工作表名(与 @ExcelEntity 一致)
        exportParams.settitle("员工信息表");    // Excel 标题(顶部大标题)

        // 使用 EasyPOI 生成 Workbook
        return ExcelExportUtil.exportExcel(exportParams, entityClass, dataList);
    }
}

2.3 Excel 转 PDF 工具类(Aspose.Cells)

使用 Aspose.Cells 加载 Excel 的 Workbook,并转换为 PDF 字节流,保留所有格式:

java 复制代码
@Component
public class ExcelToPdfConverter {
    private static final Logger log = LoggerFactory.getLogger(ExcelToPdfConverter.class);

    /**
     * 将 Workbook 转换为 PDF 字节流(Aspose.Cells)
     * @param workbook Excel Workbook
     * @return PDF 字节流
     */
    public static ByteArrayOutputStream convertToPdf(Workbook workbook) throws Exception {
        // 1. 创建 PDF 保存选项(可选配置)
        PdfSaveOptions saveOptions = new PdfSaveOptions();
        saveOptions.setOnePagePerSheet(true);       // 每个工作表一页
        saveOptions.setFormat(PdfFormat.PdfA2B);    // 符合 PDF/A-2B 标准(可选)
        saveOptions.setCompressImages(true);        // 压缩图片(减小文件大小)
        saveOptions.setImageCompression(PdfImageCompressionType.MEDIUM);  // 图片压缩级别

        // 2. 转换为 PDF 字节流
        ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
        workbook.save(pdfOutputStream, saveOptions);
        return pdfOutputStream;
    }
}

2.4 Controller 层整合(Excel 导出 PDF)

编写接口,接收请求后生成 Excel 并转换为 PDF 下载:

less 复制代码
@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. 模拟数据(实际从数据库查询)
        List<Employee> dataList = new ArrayList<>();
        dataList.add(new Employee(1L, "张三", 25, "技术部", new Date()));
        dataList.add(new Employee(2L, "李四", 30, "市场部", new Date()));

        // 2. 生成 Excel Workbook(EasyPOI)
        Workbook workbook = excelExportUtils.generateExcelWorkbook(dataList, Employee.class);

        // 3. 转换为 PDF 字节流(Aspose.Cells)
        ByteArrayOutputStream pdfOutputStream = pdfConverter.convertToPdf(workbook);

        // 4. 设置响应头(下载 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 导出 PDF 实现(EasyPOI + Aspose.Words)

3.1 定义 Word 模板(Freemarker 或直接生成)

Word 文档生成通常使用 ​​模板引擎​​(如 Freemarker),通过替换模板中的占位符生成内容。这里以 Aspose.Words 直接生成简单 Word 为例:

scss 复制代码
@Component
public class WordDocumentBuilder {
    private static final Logger log = LoggerFactory.getLogger(WordDocumentBuilder.class);

    /**
     * 构建 Word 文档(Aspose.Words)
     * @param content 文档内容(文本+表格)
     * @return Document(Aspose.Words 文档对象)
     */
    public static Document buildWordDocument(String content) throws Exception {
        // 创建空白文档
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // 写入标题
        builder.getFont().setName("微软雅黑");
        builder.getFont().setSize(16);
        builder.getFont().setBold(true);
        builder.writeln("员工信息报告");

        // 写入正文
        builder.getFont().setSize(12);
        builder.getFont().setBold(false);
        builder.writeln("以下是近期员工信息:");
        builder.writeln();

        // 写入表格(示例数据)
        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("技术部");
        builder.endRow();

        builder.endTable();

        return doc;
    }
}

3.2 Word 转 PDF 工具类(Aspose.Words)

使用 Aspose.Words 将生成的 Word 文档转换为 PDF,保留样式、图片等:

java 复制代码
@Component
public class WordToPdfConverter {
    private static final Logger log = LoggerFactory.getLogger(WordToPdfConverter.class);

    /**
     * 将 Aspose.Words Document 转换为 PDF 字节流
     * @param document Word 文档对象
     * @return PDF 字节流
     */
    public static ByteArrayOutputStream convertToPdf(Document document) throws Exception {
        // 1. 配置 PDF 保存选项(可选)
        PdfSaveOptions saveOptions = new PdfSaveOptions();
        saveOptions.setCompressFonts(true);       // 压缩字体
        saveOptions.setUseHighQualityRendering(true);  // 高质量渲染

        // 2. 转换为 PDF 字节流
        ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
        document.save(pdfOutputStream, saveOptions);
        return pdfOutputStream;
    }
}

3.3 Controller 层整合(Word 导出 PDF)

编写接口,生成 Word 文档并转换为 PDF 下载:

less 复制代码
@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. 构建 Word 文档(Aspose.Words)
        String content = "员工信息报告内容...";  // 实际从数据库或业务逻辑获取
        Document wordDoc = wordBuilder.buildWordDocument(content);

        // 2. 转换为 PDF 字节流(Aspose.Words)
        ByteArrayOutputStream pdfOutputStream = wordPdfConverter.convertToPdf(wordDoc);

        // 3. 设置响应头(下载 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();
    }
}

四、关键问题与优化

4.1 格式保留问题

  • ​Excel​:Aspose.Cells 支持所有 Excel 特性(合并单元格、条件格式、图表、公式等),转换后与原文档一致。
  • ​Word​:Aspose.Words 支持样式(字体、颜色、段落)、图片、页眉页脚、目录等,复杂排版(如多栏、文本框)也能完美保留。

4.2 性能优化

  • ​大数据量 Excel​ :若数据量超过 10 万行,建议使用 EasyPOI 的 @ExcelEntity 配合分页查询,避免内存溢出。
  • ​PDF 压缩​ :通过 PdfSaveOptions 配置图片压缩(PdfImageCompressionType)和字体嵌入策略,减小文件体积。

4.3 免费方案替代(LibreOffice)

若无法使用商业库,可通过调用 LibreOffice 命令行实现转换(需服务器安装 LibreOffice):

arduino 复制代码
// 调用 LibreOffice 转换 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 转换失败");
    }
}

​注意​​:此方法格式保留可能不完整(如复杂样式丢失),仅适用于小型项目。

五、总结

本文通过 ​​EasyPOI 生成 Excel/Word​ ​ + ​​Aspose 系列工具转换 PDF​​ 的方案,实现了 Spring Boot 中 Excel 和 Word 到 PDF 的完整流程。Aspose 工具在格式保留和性能上表现优异,适合企业级高精度需求;若预算有限,可尝试 LibreOffice 命令行方案(需接受格式限制)。实际项目中需根据数据量和格式要求选择合适方案。

​扩展资源​​:

相关推荐
Littlewith19 分钟前
Node.js:创建第一个应用
服务器·开发语言·后端·学习·node.js
梅落几点秋.24 分钟前
java-字符串和集合
java·开发语言
码间舞31 分钟前
【面试官】:NodeJs事件循环你了解多少?我笑了,让我喝口水慢慢给你说来......
后端·node.js
都叫我大帅哥33 分钟前
阿里巴巴Sentinel:高可用防护的瑞士军刀
java·spring cloud
二向箔reverse38 分钟前
Linux 文件操作命令大全:从入门到精通的实用指南
java·linux·服务器
万能小锦鲤43 分钟前
《大数据技术原理与应用》实验报告三 熟悉HBase常用操作
java·hadoop·eclipse·hbase·shell·vmware·实验报告
努力的小郑1 小时前
深入剖析异常日志:为什么你该立刻告别 `e.printStackTrace()` ?
java·apache log4j
itsoo1 小时前
2.5万字!一文搞懂稳定性建设要怎么做?
后端
一眼万年041 小时前
Nginx Master-Worker 进程间的共享内存是怎么做到通用还高效的?
后端·nginx·面试
小华同学ai1 小时前
惊喜! Github 10k+ star 的国产流程图框架,LogicFlow 能解你的图编辑痛点?
前端·后端·github