1.引入pom.xml(可能需要下载引入jar)
<dependency>
<groupId>aspose-words</groupId>
<artifactId>aspose-words</artifactId>
<scope>system</scope>
<version>19.5</version>
<systemPath>${project.basedir}/lib/aspose-words-19.5.jar</systemPath>
</dependency>
<dependency>
<groupId>aspose-cells</groupId>
<artifactId>aspose-cells</artifactId>
<scope>system</scope>
<version>21.1</version>
<systemPath>${project.basedir}/lib/aspose-cells-8.5.2.jar</systemPath>
</dependency>
2.添加授权文件,在resources下新建文件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>
4.代码工具类
package com.xmcbs.portalserver.utils;
import com.aspose.cells.*;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* @author :wangjp
* @date :2022/6/6 17:56
*/
public class ExcelToPdf {
/**
* 获取license 去除水印
* @return
*/
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("excel-license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* excel 转为pdf 输出。
*
* @param sourceFilePath excel文件
* @param desFilePathd pad 输出文件目录
*/
public static void excel2pdf(String sourceFilePath, String desFilePathd){
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
try {
Workbook wb = new Workbook(sourceFilePath);// 原始excel路径
FileOutputStream fileOS = new FileOutputStream(desFilePathd);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
int[] autoDrawSheets={3};
//当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。
// autoDraw(wb,autoDrawSheets);
int[] showSheets={0};
//隐藏workbook中不需要的sheet页。
// printSheetPage(wb,showSheets);
wb.save(fileOS, pdfSaveOptions);
fileOS.flush();
fileOS.close();
//System.out.println("转换PDF完毕!");
} catch (Exception e) {
e.printStackTrace();
}
}
public static ByteArrayOutputStream excel2pdf(InputStream pdfPath){
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return null;
}
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Workbook wb = new Workbook(pdfPath); //原始excel路径
// FileOutputStream fileOS = new FileOutputStream(desFilePathd);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
int[] autoDrawSheets={3};
//当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。
// autoDraw(wb,autoDrawSheets);
int[] showSheets={0};
//隐藏workbook中不需要的sheet页。
// printSheetPage(wb,showSheets);
wb.save(os, pdfSaveOptions);
// fileOS.flush();
// fileOS.close();
//System.out.println("转换PDF完毕!");
return os;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}