Springboot EXCEL转换为PDF——aspose方式

Springboot EXCEL转换为PDF——aspose方式

一、准备相关资源

1.下载Excel转PDF需要的aspose包

[网盘地址](https://pan.baidu.com/s/1q-CYzdDWqJPgO0331GgoOw)
提取码:uwyx

2.下载aspose破解license文件

网盘链接:https://pan.baidu.com/s/1Mq2XPMhOcXACsAwQyHYREA
提取码:211b

二、配置maven依赖

1. 将下载下来的jar包放在项目的根目录的lib文件夹下

在这里插入图片描述

2. 将下载下来的license.xml放在根目录的resources下

在这里插入图片描述

3.添加maven依赖

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

三、创建java文件并编写代码

public class Word2PdfAsposeUtil {

	public static boolean getLicenseExcel() {
        boolean result = false;
        InputStream is = null;
        try {
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:license.xml");
            is = resources[0].getInputStream();
            com.aspose.cells.License aposeLic = new com.aspose.cells.License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
   /**
     * excel转pdf
     *
     * @param inpath  excel文件地址
     * @param outPath pdf地址
     */
    public static boolean excel2pdf(String inpath, String outPath) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicenseExcel()) {
            return false;
        }
        FileOutputStream os = null;
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一个空白pdf文档
            os = new FileOutputStream(file);
            Workbook wb = new Workbook(inpath);// 原始excel路径

            wb.save(os, com.aspose.cells.SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return true;
    }

四、编写测试main方法

    public static void main(String[] args) {
        excel2pdf("E:\\123\\1.xls","E:\\123\\1.pdf");
    }
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小菜菜程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值