Java教程

Java使用Aspose将EXCEL转为PDF文档

本文主要是介绍Java使用Aspose将EXCEL转为PDF文档,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

项目需求需要将Excel文件转换成pdf文档,网上教程找到相关资料,记录一下。

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、测试

 

 

 

 

这篇关于Java使用Aspose将EXCEL转为PDF文档的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!