Java使用Aspose將EXCEL轉為PDF文檔


項目需求需要將Excel文件轉換成pdf文檔,網上教程找到相關資料,記錄一下。

破解版去水印的Aspose相關jar和證書戳這里下載(可用到2099年):下載

1、引入aspose-cells-8.5.2.jar包、license.xml破解文件

<dependency>
    <groupId>aspose-cells</groupId>
    <artifactId>aspose</artifactId>
    <version>8.5.2</version>
    <scope>system</scope>
    <systemPath>${basedir}/aspose-cells-8.5.2.jar</systemPath>
</dependency>

 

2、Excel轉為pdf工具類

package com.example.sbs.util;
 
import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
 
import java.io.FileOutputStream;
import java.io.InputStream;
 
public class Excel2Pdf {
 
    public static void main(String[] args) {
 
        String sourceFilePath="C:\\Users\\Administrator\\Desktop\\excel2pdf\\source.xlsx";
        String desFilePath="C:\\Users\\Administrator\\Desktop\\excel2pdf\\rest.pdf";
 
        excel2pdf(sourceFilePath, desFilePath);
 
    }
 
    /**
     * 獲取license 去除水印
     * @return
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = Excel2Pdf.class.getClassLoader().getResourceAsStream("\\license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    /**
     * excel 轉為pdf 輸出。
     *
     * @param sourceFilePath  excel文件
     * @param desFilePathd  pad 輸出文件目錄
     */
    public static void excel2pdf(String sourceFilePath, String desFilePathd ){
        // 驗證License 若不驗證則轉化出的pdf文檔會有水印產生
        if (!getLicense()) {
            return;
        }
        try {
            Workbook wb = new Workbook(sourceFilePath);// 原始excel路徑
 
            FileOutputStream fileOS = new FileOutputStream(desFilePathd);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);
 
 
            int[] autoDrawSheets={3};
            //當excel中對應的sheet頁寬度太大時,在PDF中會拆斷並分頁。此處等比縮放。
//            autoDraw(wb,autoDrawSheets);
 
            int[] showSheets={0};
            //隱藏workbook中不需要的sheet頁。
            printSheetPage(wb,showSheets);
            wb.save(fileOS, pdfSaveOptions);
            fileOS.flush();
            fileOS.close();
            System.out.println("完畢");
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     * 設置打印的sheet 自動拉伸比例
     * @param wb
     * @param page 自動拉伸的頁的sheet數組
     */
    public static void autoDraw(Workbook wb,int[] page){
        if(null!=page&&page.length>0){
            for (int i = 0; i < page.length; i++) {
                wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
                wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
            }
        }
    }
 
 
    /**
     * 隱藏workbook中不需要的sheet頁。
     * @param wb
     * @param page 顯示頁的sheet數組
     */
    public static void printSheetPage(Workbook wb,int[] page){
        for (int i= 1; i < wb.getWorksheets().getCount(); i++)  {
            wb.getWorksheets().get(i).setVisible(false);
        }
        if(null==page||page.length==0){
            wb.getWorksheets().get(0).setVisible(true);
        }else{
            for (int i = 0; i < page.length; i++) {
                wb.getWorksheets().get(i).setVisible(true);
            }
        }
    }
 
}
 
 

3、測試

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM