使用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

相关文章

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客