使用aspose相关包将excel转成pdf 并导出

2024-04-09 07:28

本文主要是介绍使用aspose相关包将excel转成pdf 并导出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SpringBoot 项目 基于aspose相关jar包 将excel 转换成pdf 导出

1、依赖的jar包 , jar获取链接 aspose相关三方jar ,下载解压后,在项目路径下建一个libs包,然后将下图两个jar 拷贝至刚新建的libs目录中

在这里插入图片描述

2、pom.xml中加入maven引入

        <dependency><groupId>com.aspose.cells</groupId><artifactId>cells-8.5.2 </artifactId><scope>system</scope><systemPath>${project.basedir}/src/main/resources/libs/aspose-cells-8.5.2.jar</systemPath><version>8.5.2</version></dependency><dependency><groupId>com.aspose.words</groupId><artifactId>words-15.8.0 </artifactId><scope>system</scope><systemPath>${project.basedir}/src/main/resources/libs/aspose-words-15.8.0.jar</systemPath><version>15.8.0</version></dependency>
2.1 使用SpringBoot打包插件生成jar包的时候,你会发现这个jar包不会被打进去,进而出现错误。解决这个问题就需要在maven打包插件中配置一个includeSystemScope属性
<build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><!--设置为true,以便把本地的system的jar也包括进来--><includeSystemScope>true</includeSystemScope></configuration></plugin></plugins></build>

3、编写转换工具类 如下

package com.by.excelToPdf;import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;public class PdfUtil {/*** excel 转 pdf* @param is 输入流* @return 输出流*/public static ByteArrayOutputStream excel2pdf(ByteArrayInputStream is) {ByteArrayOutputStream bos = null;try {bos = new ByteArrayOutputStream();// 验证 LicensegetLicense();Workbook wb = new Workbook(is);PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();pdfSaveOptions.setOnePagePerSheet(true);wb.save(bos, pdfSaveOptions);bos.flush();bos.close();} catch (Exception e) {System.out.println("convert failed");e.printStackTrace();}return bos;}/*** 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);}}}
}

4、调用 工具类中有基于流的方式入参、文件地址方式入参等,大家可根据自行需要选择合适的转换方法

    public static void main(String[] args) {String inputFile = "D:/testPdf/222.xlsx";String outputFile = "D:/testPdf/222.pdf";PdfUtil.excel2pdf(inputFile, outputFile);}

5、注意问题,以上转换在windows环境运行一切正常,可能部署到linux环境会存在中文乱码,引起乱码的原因可能是因为linux环境无中文相关字体

  • linux环境查看字段方法 字体路径/usr/share/fonts
# 刷新字体缓存
fc-cache
# 查看所有字体
fc-list
# 查看所有中文字体
fc-list :lang=zh
  • 如果无中文字体 我们可能把windows环境中的字段上传至linux字段目录下,windows环境字段路径C:\Windows\Fonts,上传后安装字段
yum -y install mkfontscale mkfontdir fontconfig
# mkfontscale:字体扩展、mkfontdir:新增字体目录、fc-cache:刷新缓存
mkfontscale && mkfontdir && fc-cache 
  • 如果使用docker 容器启动的应用服务,则还需要使用挂载卷的方式,将宿主体的字体和容器共享,具体方式即启动容器时 加上 “-v /usr/share/fonts/:/usr/share/fonts”

这篇关于使用aspose相关包将excel转成pdf 并导出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/887536

相关文章

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

Pydantic中Optional 和Union类型的使用

《Pydantic中Optional和Union类型的使用》本文主要介绍了Pydantic中Optional和Union类型的使用,这两者在处理可选字段和多类型字段时尤为重要,文中通过示例代码介绍的... 目录简介Optional 类型Union 类型Optional 和 Union 的组合总结简介Pyd

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核

使用Python自建轻量级的HTTP调试工具

《使用Python自建轻量级的HTTP调试工具》这篇文章主要为大家详细介绍了如何使用Python自建一个轻量级的HTTP调试工具,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录一、为什么需要自建工具二、核心功能设计三、技术选型四、分步实现五、进阶优化技巧六、使用示例七、性能对比八、扩展方向建

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经