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");
    }

猜你喜欢

转载自blog.csdn.net/qq_42900469/article/details/130767249
今日推荐