使用Java代码把Excel文件转换成PDF文件

需要引用aspose包,引入操作我写了一个博客,地址如下

https://blog.csdn.net/weixin_46713508/article/details/125495770?spm=1001.2014.3001.5502

pom全部依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.test</groupId><artifactId>aspose-word-excel</artifactId><version>1.0-SNAPSHOT</version><dependencies><!--word文件转PDF以及水印--><dependency><groupId>e-iceblue</groupId><artifactId>spire.doc.free</artifactId><version>3.9.0</version></dependency><dependency><groupId>e-iceblue</groupId><artifactId>spire.pdf.free</artifactId><version>3.9.0</version></dependency><!--解析word时用到的包--><dependency><groupId>com.lowagie</groupId><artifactId>itext</artifactId><version>2.1.7</version></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.16</version><scope>compile</scope></dependency><!--Aspose相关依赖 需要在本地指定导入--><dependency><groupId>com.aspose.words</groupId><artifactId>aspose-words</artifactId><version>words-15.8.0-jdk16</version><scope>system</scope><systemPath>${project.basedir}/src/main/resources/lib/aspose-words-15.8.0-jdk16.jar</systemPath></dependency><dependency><groupId>com.aspose.cells</groupId><artifactId>aspose-cells</artifactId><version>cell-8.5.2</version><scope>system</scope><systemPath>${project.basedir}/src/main/resources/lib/aspose-cells-8.5.2.jar</systemPath></dependency><dependency><groupId>com.aspose.pdf</groupId><artifactId>aspose-pdf</artifactId><version>pdf-17.3.0</version><scope>system</scope><systemPath>${project.basedir}/src/main/resources/lib/aspose.pdf-17.3.0.jar</systemPath></dependency><!--文件上传--><!--骑缝章相关依赖--><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency><dependency><groupId>com.itextpdf.tool</groupId><artifactId>xmlworker</artifactId><version>5.5.13</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId><version>2.0.1</version></dependency></dependencies><repositories><repository><id>com.e-iceblue</id><url>https://repo.e-iceblue.cn/repository/maven-public/</url></repository></repositories></project>

resource下创建license.xml

<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

代码如下

   /*** 获取license** @return*/public static boolean getLicense() {boolean result = false;try {InputStream is = ConvertPDFUtils.class.getClassLoader().getResourceAsStream("\\license.xml");License aposeLic = new License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}
 public static Boolean excelConvertToPdf(String excelfilePath, String pdfFilePath) {// 验证Licenseif (!getLicense()) {return false;}try {// 原始excel路径Workbook wb = new Workbook(excelfilePath);// 输出路径File pdfFile = new File(pdfFilePath);FileOutputStream fileOS = new FileOutputStream(pdfFile);wb.save(fileOS, SaveFormat.PDF);return true;} catch (Exception e) {e.printStackTrace();return false;}}
 public static void main(String[] args) {String sourcePath = "D:\\File\\体重记录.xlsx";String targetPath = "D:\\File\\体重变化图表.pdf";// 验证Licenseif (!ConvertPDFUtils.getLicense()) {return;}long old = System.currentTimeMillis();if (ConvertPDFUtils.excelConvertToPdf(sourcePath, targetPath)) {long now = System.currentTimeMillis();System.out.println("Excel转Pdf成功,共耗时:" + ((now - old) / 1000.0) + "秒");}}

直接运行即可

下面是简单封装后的,应该以流输出,目前没时间处理

    public boolean excelConvertPdf(MultipartFile file, String outPath) throws Exception {// 文件输出流FileOutputStream fileOS = null;// 验证Licenseif (!ConvertPDFUtils.getLicense()) {return false;}try {// 读取模板文件Workbook wb = new Workbook(file.getInputStream());// 输出路径File pdfFile = new File(outPath);fileOS = new FileOutputStream(pdfFile);wb.save(fileOS, SaveFormat.PDF);return true;} catch (Exception e) {e.printStackTrace();return false;}finally {fileOS.close();}}

调用如下

Java使用aspse实现Excel文件转换成PDF文件相关推荐

  1. java编程将HTML文件转换成PDF文件

    网上有好几种方法可以将将HTML文件转换成PDF文件但是有些对HTML文件格式要求比较严格,稍微错了一些就不能生成我们所要的PDF文件,这里我推荐一个 PD4ML,它可以解决HTML文件格式不正确的问 ...

  2. Python 实现office单个文件或整个文件夹(word,ppt,excel)转换成PDF文件,并获取PDF文件页数

    Python 实现office单个文件或整个文件夹(word,ppt,excel)转换成PDF文件,并获取PDF文件页数 文件夹中获取需要转换的文件数,将其全部转换或可单独转换单个文件,并获取转成PD ...

  3. 把jpg文件转换成pdf文件

    把jpg文件转换成pdf文件 生涯中,人人常常会拍摄一些照片用来忖量,可是岁月长了,时时会由于照片多而杂乱而懒于翻看,那末你是否思虑做一个电子相册,或制造一本PDF花样的书本来翻看呢?业余的这款JPG ...

  4. 怎么把jpg文件转换成pdf文件

    怎么把jpg文件转换成pdf文件 PDF转换成Word文档对我们来讲并不生僻,假设文字是由一张张图片构成的,碰着这种状况怎么办,怎么将JPG转换成PDF?菜鸟损耗了不少工夫和肉体才找到一款好用的JPG ...

  5. 如何将Ai文件转换成PDF文件

    要将Ai文件转换成PDF文件,请按照以下步骤操作: 1. 打开客汇宝网站软件并打开要转换的文件. 2. 点击"文件"菜单并选择"另存为". 3. 在" ...

  6. 怎么把图片文件转换成PDF文件

    除了office文件外,我们最常用的就是PDF文件了.因为PDF文件格式可以将文字.字型.格式.颜色及独立于 设备和分辨率的图形图像等封装在一个文件中.该格式文件还可以包含超文本链接.声音和动态影像等 ...

  7. CAJ文件转换成PDF文件:教你如何实现文件格式转换

    CAJ文件是中国知网常用的一种文献格式,但有时候我们需要将其转换成PDF格式,以方便阅读.打印.分享等用途.本文将教你如何实现CAJ文件转换成PDF文件. 方法一.使用CAJViewer软件 步骤1: ...

  8. 怎么将知网下载的CAJ文件转换成PDF文件

    我们熟知的CAJ文件必须使用中国知网的CAJ Viewer才能打开,但是如果想以PDF的格式查看论文那就需要将CAJ文件转换为pdf格式,那么怎么CAJ文件转换成PDF文件呢? 转换方法其实很简单,这 ...

  9. 怎么把caj文件转换成pdf文件

    我们在网上下载素材的时候,就有很大一部分是caj格式的文件,我们又想将caj文件整理成pdf文件,那么怎么把caj文件转换成pdf文件呢? 其实转换很简单,只需要一个CAJ转换器就能实现,下面介绍ca ...

最新文章

  1. 又一次 Java 内存泄漏排查,新技能+1
  2. 【Cocos2dx开发】精灵
  3. 《Windows核心编程》---邮槽通信
  4. 北大计算机博进高校,他是北大第一位博士,留校任教却连做三件“傻事”,博导都没评上...
  5. 后缀数组 --- HDU 3518 Boring counting
  6. sed查找文件包含字段_sed(查找替换) 与awk(提取字段)
  7. 在switch语句中使用字符串以及实现原理
  8. sql server 锁定_关于锁定SQL Server的全部
  9. 起底方舟编译器的引用计数!
  10. python两组数的差异_Python中两个日期之间的差异
  11. Swashbuckle Swagger组件扩展
  12. cisco CEF的一点概述
  13. hadoop提交作业到云端问题解决
  14. PSP2000下实现完美截屏
  15. USB接线定义和链接摄像头
  16. Pray for 京阿尼——愿逝者安息,伤者早日康复
  17. 用python创建及使用一个tls_Python使用TLSv1.2和Forward Secrecy粘贴SSL服务器
  18. 多档电位器换挡不准确问题
  19. 苹果发通谍拒绝“热更新”,中国程序猿“最受伤”
  20. ChIP-seq基础入门

热门文章

  1. 科技公司的老板非得学编程吗?
  2. TYVJ 1074 武士风度的牛
  3. 腾讯云CentOS7.0使用yum安装mysql及使用遇到的问题
  4. python免费开源工具推荐_年薪200万的程序员,推荐的10大Python开源免费工具!
  5. 武邑中学2021高考成绩查询,拼百日酬壮志 誓圆梦铸辉煌——河北武邑中学2021年高三年级决胜高考誓师大会...
  6. 基于STM32F407无线通信实验(有代码)
  7. 【搞定GTD】时间日志的工具从Momento迁移到EverNote上
  8. yolov5 onnx 前后处理+运行推理(暂记)
  9. opencv 表格识别之表格透视矫正(二)
  10. eclipse java启动参数_Eclipse启动参数设置