开发者

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

目录
  • 一、环境准备与依赖配置
    • 1.1 方案选型
    • 1.2 依赖配置(商业库方案)
  • 二、Excel 导出 PDF 实现(EasyPOI + ASPose.Cells)
    • 2.1 定义 Excel 实体类(EasyPOI 注解)
    • 2.2 Excel 生成工具类(EasyPOI)
    • 2.3 Excel 转 PDF 工具类(Aspose.Cells)
    • 2.4 Controller 层整合(Excel 导出 PDF)
  • 三、Word 导出 PDF 实现(EasyPOI + Aspose.Words)
    • 3.1 定义 Word 模板(Freemarker 或直接生成)
    • 3.2 Word 转 PDF 工具类(Aspose.Words)
    • 3.3 Controller 层整合(Word 导出 PDF)
  • 四、关键问题与优化
    • 4.1 格式保留问题
    • 4.2 性能优化
    • 4.3 免费方案替代(LibreOffice)
  • 五、总结

    在企业级开发中,将 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.devze.com </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;
    编程客栈
        @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.devze.compose.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的资料请关注编程客栈(www.devze.com)其它相关文章!

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜