SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

2025-07-18 19:50

本文主要是介绍SpringBoot+EasyPOI轻松实现Excel和Word导出PDF,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实...

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

一、环境准备与依赖配置

1.1 方案选型

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

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

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

pom.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>
   http://www.chinasem.cn </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 生成逻辑,支持标题、表头、数据校验等:

@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;
China编程
    @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 对象:

@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 字节流,保留所有格式:

@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 下载:

@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 为例:

@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,保留样式、图片等:

@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 下载:

@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​​:Ashttp://www.chinasem.cnpose.Words 支持样式(字体、颜色、段落)、图片、页眉页脚、目录等,复杂排版(如多栏、文本框)也能完美保留。

4.2 性能优化

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

4.3 免费方案替代(LibreOffice)

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

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

以上就是SpringBoot+EasyPOI轻松实现Excel和Word导出PDF的详细内容,更多关于SpringBoot Excel和Word导出PDF的资料请关注编程China编程(www.chinasem.cn)其它相关文章!

这篇关于SpringBoot+EasyPOI轻松实现Excel和Word导出PDF的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1155423

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4