java将excel转成pdf

分享一个简单的excel转pdf

1、引用aspose-cells工具

<dependency>
    <groupId>com.zy</groupId>
    <artifactId>aspose-cells</artifactId>
    <version>17.8.0</version>
</dependency>

2、由于转换后会产生水印,去除PDF水印,需要进行权限认证,权限认证文件是license.xml,已经放在网盘

链接:https://pan.baidu.com/s/1wgdQD1UfYmJLf5iES5la7Q
提取码:kj64

3、话不多说,上代码

复制代码
package com.zy.jeerdp.modules.ar.utils;

import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.zy.jeerdp.common.utils.IdGen;

import java.io.*;


public class Excel2PdfUtil {
    /**
     * excel文件转换为PDF文件
     * @param Address 需要转化的Excel文件地址,
     * @param outputPath 转换后的文件地址
     */
    public static String excel2pdf(String Address,String outputPath) {
        String fileName = IdGen.uuid() + ".pdf";
        if (!getLicense()) {   // 验证License 若不验证则转化出的pdf文档会有水印产生
            return null;
        }
        try {
            FileWriter writer = new FileWriter(outputPath + fileName);
            writer.close();
            File pdfFile = new File(outputPath + fileName); // 输出路径
            FileInputStream excelstream = new FileInputStream(Address);
            Workbook wb = new Workbook(excelstream);// excel路径,这里是先把数据放进缓存表里,然后把缓存表转化成PDF
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);//参数true把内容放在一张PDF页面上;
            wb.save(fileOS, pdfSaveOptions);
            fileOS.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }

    //获取认证,去除水印
    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = Excel2PdfUtil.class.getClassLoader().getResourceAsStream("pdfLicense/license.xml");//这个文件应该是类似于密码验证(证书?),用于获得去除水印的权限
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
复制代码

 

posted @   gz、qml  阅读(3965)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示