Excel转PDF工具类

public class ExcelToPDF {

    /**
     * 获取license
     *
     * @return
     */
    private static boolean getLicense() {
        boolean result = false;
        try {
            InputStream license = ExcelToPDF.class.getClassLoader().getResourceAsStream("\\license.xml");// license路径
            License aposeLic = new License();
            aposeLic.setLicense(license);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void excel2Pdf(InputStream inputStream, OutputStream outputStream){
        long old = System.currentTimeMillis();
        // 验证License
        if (!getLicense()) {
            return;
        }
        try {
            Workbook workbook = new Workbook(inputStream);

            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            //把内容放在一张PDF 页面上;
            pdfSaveOptions.setOnePagePerSheet(false);
            pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);

            pdfSaveOptions.setFontSubstitutionCharGranularity(true);
            //  只放一张纸;我的专为横向了
            workbook.save(outputStream, pdfSaveOptions);
            long now = System.currentTimeMillis();
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param excelPath 文件路径
     * @param outputStream
     */
    public static void excel2Pdf(String excelPath, OutputStream outputStream) {
        FileInputStream fileInputStream = null;
        try {
            File excelFile = new File(excelPath);
            if (excelFile.exists()) {
                fileInputStream = new FileInputStream(excelFile);
                excel2Pdf(fileInputStream,outputStream);
            }else{
                System.out.println("文件不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void excel2Pdf(String excelPath, String pdfPath) {
        File pdfFile = new File(pdfPath);
        FileOutputStream fileOS = null;
        try {
            fileOS = new FileOutputStream(pdfFile);
            excel2Pdf(excelPath,fileOS);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (fileOS != null) {
                try {
                    fileOS.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static File mulFile2One(List<File> files, String targetPath) throws IOException{
        // pdf合并工具类
        PDFMergerUtility mergePdf = new PDFMergerUtility();
        for (File f : files) {
            if(f.exists() && f.isFile()){
                // 循环添加要合并的pdf
                mergePdf.addSource(f);
            }
        }
        // 设置合并生成pdf文件名称
        mergePdf.setDestinationFileName(targetPath);
        // 合并pdf
        mergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
        return new File(targetPath);
    }

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        excel2Pdf("/Users/hangchun.shen/Documents/sja/321.xlsx","/Users/hangchun.shen/Downloads/ToPDF11.pdf");
//        excel2Pdf("/Users/hangchun.shen/Documents/sja/456.xlsx","/Users/hangchun.shen/Downloads/ToPDF9.pdf");

//        List<File> files = new ArrayList <>();
//
//        File a = new File("/Users/hangchun.shen/Downloads/ToPDF8.pdf");
//        File b = new File("/Users/hangchun.shen/Downloads/ToPDF9.pdf");
//        files.add(b);
//        files.add(a);
//        mulFile2One(files,"/Users/hangchun.shen/Downloads/ToPDF10.pdf");
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值