最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot+EasyPOI輕松實(shí)現(xiàn)Excel和Word導(dǎo)出PDF

 更新時(shí)間:2025年07月18日 15:07:40   作者:奇妙智能  
在企業(yè)級(jí)開(kāi)發(fā)中,將 Excel 和 Word 文檔導(dǎo)出為 PDF 是常見(jiàn)需求,本文將結(jié)合 ??EasyPOI和 ??Aspose 系列工具實(shí)現(xiàn) Excel 和 Word 到 PDF 的轉(zhuǎn)換,需要的可以了解下

在企業(yè)級(jí)開(kāi)發(fā)中,將 Excel 和 Word 文檔導(dǎo)出為 PDF 是常見(jiàn)需求(如報(bào)表歸檔、合同存檔)。本文將結(jié)合 ??EasyPOI(Excel 快速導(dǎo)出)?? 和 ??Aspose 系列工具(格式完美轉(zhuǎn)換)??,詳細(xì)講解如何實(shí)現(xiàn) Excel 和 Word 到 PDF 的轉(zhuǎn)換,并解決格式保留、性能優(yōu)化等核心問(wèn)題。

一、環(huán)境準(zhǔn)備與依賴配置

1.1 方案選型

步驟工具/庫(kù)特點(diǎn)適用場(chǎng)景
??Excel 生成??EasyPOI(阿里)基于 Apache POI 優(yōu)化,支持注解快速生成 Excel,內(nèi)存占用低數(shù)據(jù)量中等(十萬(wàn)行以內(nèi))
??Excel 轉(zhuǎn) PDF??Aspose.Cells(商業(yè))完美保留 Excel 格式(字體、顏色、合并單元格、圖表等),性能強(qiáng)企業(yè)級(jí)高精度轉(zhuǎn)換需求
??Word 生成??Apache POI(XWPF)開(kāi)源,支持 .docx 格式,需手動(dòng)處理復(fù)雜樣式簡(jiǎn)單 Word 文檔(文本+表格)
??Word 轉(zhuǎn) PDF??Aspose.Words(商業(yè))完美保留 Word 格式(樣式、圖片、頁(yè)眉頁(yè)腳等),兼容性強(qiáng)企業(yè)級(jí)高精度轉(zhuǎn)換需求

1.2 依賴配置(商業(yè)庫(kù)方案)

??注意??:Aspose 系列工具需購(gòu)買商業(yè)許可證(試用版有水?。?,測(cè)試階段可使用 免費(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 的注解簡(jiǎn)化 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)

編寫(xiě)工具類,封裝 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è)工作表一頁(yè)
        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)

編寫(xiě)接口,接收請(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ù)庫(kù)查詢)
        List<Employee> dataList = new ArrayList<>();
        dataList.add(new Employee(1L, "張三", 25, "技術(shù)部", new Date()));
        dataList.add(new Employee(2L, "李四", 30, "市場(chǎng)部", 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),通過(guò)替換模板中的占位符生成內(nèi)容。這里以 Aspose.Words 直接生成簡(jiǎn)單 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);

        // 寫(xiě)入標(biāo)題
        builder.getFont().setName("微軟雅黑");
        builder.getFont().setSize(16);
        builder.getFont().setBold(true);
        builder.writeln("員工信息報(bào)告");

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

        // 寫(xiě)入表格(示例數(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)

編寫(xiě)接口,生成 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ù)庫(kù)或業(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)鍵問(wèn)題與優(yōu)化

4.1 格式保留問(wèn)題

  • ??Excel??:Aspose.Cells 支持所有 Excel 特性(合并單元格、條件格式、圖表、公式等),轉(zhuǎn)換后與原文檔一致。
  • ??Word??:Aspose.Words 支持樣式(字體、顏色、段落)、圖片、頁(yè)眉頁(yè)腳、目錄等,復(fù)雜排版(如多欄、文本框)也能完美保留。

4.2 性能優(yōu)化

  • ??大數(shù)據(jù)量 Excel??:若數(shù)據(jù)量超過(guò) 10 萬(wàn)行,建議使用 EasyPOI 的 @ExcelEntity 配合分頁(yè)查詢,避免內(nèi)存溢出。
  • ??PDF 壓縮??:通過(guò) PdfSaveOptions 配置圖片壓縮(PdfImageCompressionType)和字體嵌入策略,減小文件體積。

4.3 免費(fèi)方案替代(LibreOffice)

若無(wú)法使用商業(yè)庫(kù),可通過(guò)調(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é)

本文通過(guò) ??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)文章!

相關(guān)文章

  • openFeign服務(wù)之間調(diào)用保持請(qǐng)求頭信息處理方式

    openFeign服務(wù)之間調(diào)用保持請(qǐng)求頭信息處理方式

    這篇文章主要介紹了openFeign服務(wù)之間調(diào)用保持請(qǐng)求頭信息處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • JavaWeb實(shí)現(xiàn)自動(dòng)登錄功能

    JavaWeb實(shí)現(xiàn)自動(dòng)登錄功能

    這篇文章主要為大家詳細(xì)介紹了JavaWeb實(shí)現(xiàn)自動(dòng)登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • springboot @Value實(shí)現(xiàn)獲取計(jì)算機(jī)中絕對(duì)路徑文件的內(nèi)容

    springboot @Value實(shí)現(xiàn)獲取計(jì)算機(jī)中絕對(duì)路徑文件的內(nèi)容

    這篇文章主要介紹了springboot @Value實(shí)現(xiàn)獲取計(jì)算機(jī)中絕對(duì)路徑文件的內(nèi)容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2021-09-09
  • JAVA 根據(jù)身份證計(jì)算年齡的實(shí)現(xiàn)代碼

    JAVA 根據(jù)身份證計(jì)算年齡的實(shí)現(xiàn)代碼

    這篇文章主要介紹了JAVA 根據(jù)身份證計(jì)算年齡的實(shí)例代碼及java根據(jù)出生日期獲得年齡的方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-05-05
  • Java中求Logn/log2 的精度問(wèn)題

    Java中求Logn/log2 的精度問(wèn)題

    這篇文章主要介紹了Java中求Logn/log2 的精度問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java實(shí)現(xiàn)時(shí)間戳轉(zhuǎn)代碼運(yùn)行時(shí)長(zhǎng)

    Java實(shí)現(xiàn)時(shí)間戳轉(zhuǎn)代碼運(yùn)行時(shí)長(zhǎng)

    這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)時(shí)間戳轉(zhuǎn)代碼運(yùn)行時(shí)長(zhǎng)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-06-06
  • java實(shí)現(xiàn)多圖片上傳功能

    java實(shí)現(xiàn)多圖片上傳功能

    這篇文章主要為大家詳細(xì)介紹了java多圖片同步上傳功能的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Java調(diào)用Python代碼的幾種方法小結(jié)

    Java調(diào)用Python代碼的幾種方法小結(jié)

    Python語(yǔ)言有豐富的系統(tǒng)管理、數(shù)據(jù)處理、統(tǒng)計(jì)類軟件包,因此從java應(yīng)用中調(diào)用Python代碼的需求很常見(jiàn)、實(shí)用,本文介紹幾種方法從java調(diào)用Python代碼,從而最大化利用兩個(gè)語(yǔ)言的優(yōu)勢(shì),需要的朋友可以參考下
    2025-01-01
  • logback-spring.xml的內(nèi)容格式詳解

    logback-spring.xml的內(nèi)容格式詳解

    這篇文章主要介紹了logback-spring.xml的內(nèi)容格式詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的的朋友參考下吧
    2023-11-11
  • IDEA中設(shè)置代碼自動(dòng)提示為Alt+/的具體做法

    IDEA中設(shè)置代碼自動(dòng)提示為Alt+/的具體做法

    很多公司都強(qiáng)制性要求使用Intellij?IDEA,其實(shí)Intellij?IDEA也確實(shí)很好用,但是一下子從Eclipse跳轉(zhuǎn)到Intellij?IDEA轉(zhuǎn)也是需要一段時(shí)間的,為了迎合之前的習(xí)慣,就需要在Intellij?IDEA中改變一些設(shè)置,如代碼自動(dòng)生成,本文給大家分享設(shè)置方法,感興趣的朋友一起看看吧
    2023-01-01

最新評(píng)論

枞阳县| 霞浦县| 通化市| 古交市| 东乌珠穆沁旗| 邵阳县| 化州市| 潮安县| 新丰县| 巫山县| 婺源县| 即墨市| 响水县| 连江县| 平阳县| 宿迁市| 伽师县| 祁东县| 浙江省| 洛扎县| 咸丰县| 陵川县| 安顺市| 新干县| 铜川市| 衡南县| 石泉县| 株洲市| 陇西县| 林口县| 云安县| 澄城县| 隆尧县| 莱阳市| 普宁市| 会宁县| 永城市| 水城县| 海淀区| 闸北区| 安阳市|