当前位置: 首页 > news >正文

使用Aspose技术将Excel/Word转换为PDF

简介:本文将介绍如何使用Aspose技术将Excel文件转换为PDF格式。我们将使用Aspose-Cells-8.5.2.jar包,并演示Java代码以及进行测试。

一、Aspose技术概述

Aspose是一款强大的文档处理库,支持多种编程语言,如JavaC#Python等。它提供了丰富的功能,可以轻松地实现各种文档格式之间的转换,包括WordExcelPowerPointPDF等。在本文中,我们将重点关注如何利用Aspose技术将Excel以及Word文件转换为PDF格式。

二、准备环境

2.1 Excel

要使用Aspose技术,首先需要下载并安装Aspose-Cells-8.5.2.jar包。您可以从Aspose官方网站免费下载这个库。也可以听过下面的maven坐标引入对应的依赖信息。

<dependency><groupId>com.aspose</groupId><artifactId>aspose-cells</artifactId><version>8.5.2</version>
</dependency>
2.2 Word

要使用Aspose技术,首先需要下载并安装Aspose-words-15.8.0.jar包。您可以从Aspose官方网站免费下载这个库。也可以听过下面的maven坐标引入对应的依赖信息。

<dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>15.8.0</version>
</dependency>

三、代码示例

3.1 excel-license.xml

注意:使用前,然后把excel-license.xml放在resources目录下,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>
3.2 word-license.xml

注意:使用前,然后把excel-license.xml放在resources目录下,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>

接下来,我们将编写一个简单的Java程序,用于将Excel文件转换为PDF文件。以下是完整的Java代码示例:

3.2 PdfUtil工具类
public class Excel2PdfUtil {/*** excel 转 pdf** @param excelFilePath excel文件路径*/public static void excel2pdf(String excelFilePath) {excel2pdf(excelFilePath, null, null);}/*** excel 转 pdf** @param excelFilePath excel文件路径* @param convertSheets 需要转换的sheet*/public static void excel2pdf(String excelFilePath, int[] convertSheets) {excel2pdf(excelFilePath, null, convertSheets);}/*** excel 转 pdf** @param excelFilePath excel文件路径* @param pdfFilePath   pdf文件路径*/public static void excel2pdf(String excelFilePath, String pdfFilePath) {excel2pdf(excelFilePath, pdfFilePath, null);}/*** excel 转 pdf** @param excelFilePath excel文件路径* @param pdfFilePath   pdf文件路径* @param convertSheets 需要转换的sheet*/public static void excel2pdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {try {pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;// 验证 LicensegetLicense();Workbook wb = new Workbook(excelFilePath);FileOutputStream fileOS = new FileOutputStream(pdfFilePath);PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();pdfSaveOptions.setOnePagePerSheet(true);if (null != convertSheets) {printSheetPage(wb, convertSheets);}wb.save(fileOS, pdfSaveOptions);fileOS.flush();fileOS.close();System.out.println("convert success");} catch (Exception e) {System.out.println("convert failed");e.printStackTrace();}}/*** 获取 生成的 pdf 文件路径,默认与源文件同一目录** @param excelFilePath excel文件* @return 生成的 pdf 文件*/private static String getPdfFilePath(String excelFilePath) {return excelFilePath.split(".")[0] + ".pdf";}/*** 获取 license 去除水印* 若不验证则转化出的pdf文档会有水印产生*/private static void getLicense() {String licenseFilePath = "excel-license.xml";try {InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream(licenseFilePath);License license = new License();license.setLicense(is);} catch (Exception e) {System.out.println("license verify failed");e.printStackTrace();}}/*** 隐藏workbook中不需要的sheet页。** @param sheets 显示页的sheet数组*/private static void printSheetPage(Workbook wb, int[] sheets) {for (int i = 1; i < wb.getWorksheets().getCount(); i++) {wb.getWorksheets().get(i).setVisible(false);}if (null == sheets || sheets.length == 0) {wb.getWorksheets().get(0).setVisible(true);} else {for (int i = 0; i < sheets.length; i++) {wb.getWorksheets().get(i).setVisible(true);}}}
}
3.3 Excel测试

在我们的resources文件夹下新建一个test文件夹,文件夹放入咱的excel文件

public class TestExcel2Pdf {public static void main(String[] args) {String srcFilePath = "/test/excel数据.xlsx";Excel2PdfUtil.excel2pdf(srcFilePath);}
}

测试结果:
在这里插入图片描述
转换成对应的PDF,几乎一模一样,如下图所示:
在这里插入图片描述

3.4 下面贴上Word的代码

工具类:

@Slf4j
public class Doc2PdfUtil {/*** 获取 license 去除水印* 若不验证则转化出的pdf文档会有水印产生*/private static void getLicense() {String licenseFilePath = "word-license.xml";try {InputStream is = DocUtil.class.getClassLoader().getResourceAsStream(licenseFilePath);License license = new License();license.setLicense(Objects.requireNonNull(is));} catch (Exception e) {log.error("license verify failed");e.printStackTrace();}}/*** word 转 pdf* @param wordFile word 文件路径* @param pdfFile  生成的 pdf 文件路径*/public static void word2Pdf(String wordFile, String pdfFile) {File file = new File(pdfFile);if (!file.getParentFile().exists()) {file.getParentFile().mkdir();}getLicense();try (FileOutputStream os = new FileOutputStream(new File(pdfFile))) {Document doc = new Document(wordFile);doc.save(os, SaveFormat.PDF);} catch (Exception e) {log.error("word转pdf失败", e);e.printStackTrace();}}
}

测试类:

public class TestWord2Pdf {public static void main(String[] args) {String docFilePath = "/test/word数据.doc";String pdfFilePath = "/test/转换pdf数据.pdf";Doc2PdfUtil.word2Pdf(docFilePath, pdfFilePath);}
}

四、总结

通过本文的介绍,您应该已经了解了如何使用Aspose技术将ExcelWord文件转换为PDF格式。本文只是讲解了使用Apose技术进行转换成PDF,其实该技术能够做的事情还有很多,可以轻松地实现各种文档格式之间的转换。

相关文章:

  • 用Python实现奇怪的疯狂按键需求
  • 风机5G智能制造工厂工业物联数字孪生平台,推进制造业数字化转型
  • 每日一题——Python实现PAT乙级1037 在霍格沃茨找零钱(举一反三+思想解读+逐步优化)
  • 代码随想录——修建二叉搜素树(Leetcode669)
  • UI 自动化分布式测试 -Docker Selenium Grid
  • SpringEvent事件发布订阅Demo
  • 【开源】课程智能组卷系统 SSM+JSP+MySQL
  • 深度学习各算法的优缺点和适用场景!!纯干货,建议收藏。(下篇)
  • Fetch API
  • 网络基础_02
  • Flutter 中的 CupertinoUserInterfaceLevel 小部件:全面指南
  • SQL查询的优化方案
  • Android 高德地图API(新版)
  • Einstein Summation 爱因斯坦求和 torch.einsum
  • 重学java 63.IO流 字节流 ④ 文件复制
  • FineReport中如何实现自动滚屏效果
  • JAVA_NIO系列——Channel和Buffer详解
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • leetcode98. Validate Binary Search Tree
  • Transformer-XL: Unleashing the Potential of Attention Models
  • 持续集成与持续部署宝典Part 2:创建持续集成流水线
  • 计算机在识别图像时“看到”了什么?
  • 警报:线上事故之CountDownLatch的威力
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 力扣(LeetCode)21
  • 判断客户端类型,Android,iOS,PC
  • 容器服务kubernetes弹性伸缩高级用法
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 小程序开发中的那些坑
  • 新手搭建网站的主要流程
  • 用mpvue开发微信小程序
  • 在weex里面使用chart图表
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • # 深度解析 Socket 与 WebSocket:原理、区别与应用
  • #Z2294. 打印树的直径
  • (04)Hive的相关概念——order by 、sort by、distribute by 、cluster by
  • (145)光线追踪距离场柔和阴影
  • (Charles)如何抓取手机http的报文
  • (CPU/GPU)粒子继承贴图颜色发射
  • (Java企业 / 公司项目)点赞业务系统设计-批量查询点赞状态(二)
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (面试必看!)锁策略
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (源码分析)springsecurity认证授权
  • (转载)跟我一起学习VIM - The Life Changing Editor
  • .dat文件写入byte类型数组_用Python从Abaqus导出txt、dat数据
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .net 7和core版 SignalR
  • .net Application的目录
  • .NET COER+CONSUL微服务项目在CENTOS环境下的部署实践
  • .NET CORE 2.0发布后没有 VIEWS视图页面文件
  • .NET Core 通过 Ef Core 操作 Mysql
  • .NET 的静态构造函数是否线程安全?答案是肯定的!