添加依赖

<dependency><groupId>com.luhuiguo</groupId><artifactId>aspose-cells</artifactId><version> 22.4</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.6</version></dependency>

代码

package com.example.excelpdf.util;import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;public class PdfUtil {/*** 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);}PdfSecurityOptions securityOptions =  new PdfSecurityOptions();// pdf权限配置securityOptions.setPrintPermission(true);//打印权限securityOptions.setUserPassword("123");securityOptions.setOwnerPassword("456");pdfSaveOptions.setSecurityOptions(securityOptions);wb.save(fileOS, pdfSaveOptions);fileOS.flush();fileOS.close();System.out.println("生成pdf成功");} catch (Exception e) {System.out.println("生成pdf失败");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() {try {String licenseXml = "<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>";ByteArrayInputStream is = new ByteArrayInputStream(licenseXml.getBytes());com.aspose.cells.License license = new com.aspose.cells.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);}}}
}

controller调用

 @RequestMapping("/3")public String a(){String filePaths="C:\\Users\\dell\\Desktop\\日志 (1).xls";File tempFile =new File( filePaths.trim());String fileName=tempFile.getName().substring(0,tempFile.getName().indexOf("."));String pdfPath="D:/pic/"+fileName+".pdf";PdfUtil.excel2pdf(filePaths, pdfPath);//filePaths需要转换的文件位置 pdfPath为存储位置return "11111";}

解密:

添加pdfbox依赖

<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.5</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.1</version></dependency><dependency><groupId>org.apache.pdfbox</groupId><artifactId>fontbox</artifactId><version>2.0.5</version></dependency>
解密时 PDDocument load = PDDocument.load(file, ownerPassWord);有可能会抛异常

java.lang.NotClassFoundError:org/bouncycastle/jce/provider/BouncyCastleProvider

解决方案:

maven添加依赖

<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk16 -->
<dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk16</artifactId><version>1.46</version>
</dependency>

java使用aspose实现Excel转PDF加入密码保护并解密相关推荐

  1. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  2. java 处理word,excel,pdf -javacode

    java 处理word,excel,pdf -javacode 2008年08月27日 星期三 01:08 P.M. java 处理word,excel,pdf -javacode 很多人问到如何抽取 ...

  3. Java准确获取Word/Excel/PPT/PDF的页数(附Word页数读不准的处理办法)

    Java准确获取Word/Excel/PPT/PDF的页数(附Word页数读不准的处理办法) 1.需求背景 2.环境准备工作 2.1 JACOB介绍及安装 2.2 Microsoft Office W ...

  4. java采用Jacob将Excel转PDF

    注意事项:使用此方法需安装Office import java.io.File; import java.util.Date;import com.jacob.activeX.ActiveXCompo ...

  5. java为word、excel、pdf、ppt、图片添加图片水印(文字水印同理)

    使用idea开发,所需依赖如下: spire的下载.使用,代码中会给出网址.idea中选中右键,添加为库即可使用 <!--使用spire,导入的jar--> <dependency& ...

  6. Java中导入/导出excel,导出pdf报表信息

    1.项目中经常需要用到报表生成,信息导入数据库的功能.主要有以下几种. 2.其中比较简单的是 外部数据无需处理直接 导入数据库中,这种比较简单.直接利用Navicat数据库工具 导入外部.示例如下 1 ...

  7. java操作word、excel、pdf 下载添加水印

    1.pdf添加水印 pom文件引入依赖 <dependency><groupId>com.itextpdf</groupId><artifactId>i ...

  8. java通过aspose实现word转pdf,加水印

    引入jar <!-- word转pdf--><dependency><groupId>com.aspose.words</groupId><art ...

  9. java通过poi导出excel和pdf

    [背景] 由于各户的需求,所以需要增加导出excel这个功能,其实大部分系统都需要这个导出功能的,所以这里也就不详细说明具体导出的背景了O(∩_∩)O~ 干完导出excel将现有的导出pdf也进行了独 ...

最新文章

  1. ORA-06502 when awr report produce
  2. boot项目中pom依赖已经删除了但是maven上还是报红线_Java Web项目是怎么跑起来的?...
  3. python有道翻译接口翻译页面-tornado框架学习及借用有道翻译api做自动翻译页面...
  4. Ubuntu环境下挂载新硬盘 --硬盘要挂载在某个文件夹下面
  5. Envi IDL中多元线性回归计算方法
  6. Google搜索技巧总结
  7. spring cloud(2)---微服务写的最全的一篇文章
  8. 软考信息系统项目管理师_管理科学(运筹学)---软考高级之信息系统项目管理师033
  9. (Object detection)目标检测从入门到精通——第四部分anchor box
  10. css属性选择器,[],=, ~=, ^=, ~=, $=, |=等符号含义
  11. angularJS添加form验证:自定义验证
  12. tensorflow手动实现算法之三逻辑回归
  13. OverFeat(译)
  14. 单龙芯3A3000-7A1000PMON研究学习-(4)撸起袖子干-makefile(a)
  15. C# 将方形图片剪切为圆形(winForm)
  16. 关于深度学习优化器 optimizer 的选择,你需要了解这些
  17. 敏捷软件开发实践-客户合作胜过合同谈判
  18. prometheus-community-PushProx介绍
  19. win10计算机右键属性打不开,win10电脑系统属性打不开的解决方法
  20. 10GHz带宽/USB3.1芯片,AW3410S高速切换开关系列方案及产品介绍

热门文章

  1. 计算机二进制运算符,二进制布尔运算
  2. 常用设计模式——装饰者模式
  3. ubuntu 18.04-设置合上笔记本盖子不休眠
  4. 2203-python基础知识
  5. [转]ZBrush3官方中文教程 一
  6. scala 惰性函数
  7. opcode php 5.4,为PHP5.4开启Zend OPCode缓存
  8. unix环境中以下列出的oracle,浙江中医药大学2012研究生计算机复习题
  9. DevOps名言警句 - 2021
  10. iOS开发:GitHub上传代码错误提示fatal: Authentication failed for 'https://gitee.com/XXX/XXX.git/‘的解决方法