前提
上一篇我们用到了word转pdf的工具,这一篇我们会拿excel做例子进行转pdf。
环境准备
<dependencies>
<dependency>
<groupId>aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
jar包的网盘链接和提取码
链接:https://pan.baidu.com/s/1vgAa1vlBtDgNT00rVBRi2Q?pwd=7284
提取码:7284
--来自百度网盘超级会员V6的分享
将jar包导入本地Maven仓库,命令如下:
mvn install:install-file -Dfile=刚下载的jar包的位置 -DgroupId=依赖里写的groupId -DartifactId=依赖里写的artifactId -Dversion=上面的version -Dpackaging=jar
示例:
mvn install:install-file -Dfile=F:\项目\文件预览\java_application_aspose_demo-master\java_application_aspose_demo-master\lib\aspose-cells-8.5.2.jar -DgroupId=aspose -DartifactId=aspose-cells -Dversion=8.5.2 -Dpackaging=jar
准备工作已完成,开始上代码。
项目代码
import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* excel转换为pdf的工具类
*
* @author shmily
*/
public class Excel2PdfUtil {
/**
* 许可证字符串
*/
private static final String LICENSE = "<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>";
/**
* 设置 license 去除水印
*/
private static void setLicense() {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(LICENSE.getBytes());
License license = new License();
license.setLicense(byteArrayInputStream);
}
/**
* excel 转 pdf
*
* @param excelFilePath excel文件路径
* @param pdfFilePath pdf文件路径
* @param convertSheets 需要转换的sheet
*/
public static void excelConvertPdf(String excelFilePath, String pdfFilePath, int[] convertSheets) {
FileOutputStream fileOutputStream = null;
try {
pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
// 设置License
setLicense();
// 读取excel文件
Workbook wb = new Workbook(excelFilePath);
fileOutputStream = new FileOutputStream(pdfFilePath);
// 设置pdf格式
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOutputStream, pdfSaveOptions);
fileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
assert fileOutputStream != null;
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* excel 转 pdf 二进制流
*
* @param excelFilePath excel文件路径
* @param pdfFilePath pdf文件路径
* @param convertSheets 需要转换的sheet
*/
public static void excelConvertPdfByte(String excelFilePath, String pdfFilePath, int[] convertSheets) {
FileOutputStream fileOutputStream = null;
try {
pdfFilePath = pdfFilePath == null ? getPdfFilePath(excelFilePath) : pdfFilePath;
// 设置License
setLicense();
// 读取excel文件
Workbook wb = new Workbook(excelFilePath);
fileOutputStream = new FileOutputStream(pdfFilePath);
// 设置pdf格式
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
if (null != convertSheets) {
printSheetPage(wb, convertSheets);
}
wb.save(fileOutputStream, pdfSaveOptions);
fileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
assert fileOutputStream != null;
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获取 生成的 pdf 文件路径,默认与源文件同一目录
*
* @param excelPath excel文件
* @return 生成的 pdf 文件
*/
private static String getPdfFilePath(String excelPath) {
int lastIndexOfPoint = excelPath.lastIndexOf(".");
String pdfFilePath = "";
if (lastIndexOfPoint > -1) {
pdfFilePath = excelPath.substring(0, lastIndexOfPoint);
}
return pdfFilePath + ".pdf";
}
/**
* 隐藏workbook中不需要的sheet页。
*
* @param sheets 显示页的sheet数组
*/
private static void printSheetPage(Workbook wb, int[] sheets) {
// 隐藏非第一个sheet页
for (int i = 1; i < wb.getWorksheets().getCount(); i++) {
wb.getWorksheets().get(i).setVisible(false);
}
// 设置显示的sheet页
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);
}
}
}
import alp.starcode.utils.Excel2PdfUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FileController {
@GetMapping("excel2Pdf")
public void excel2Pdf(String excelPdf) {
Excel2PdfUtil.excelConvertPdf("D:\\code\\pdf\\用户信息.xls", "D:\\code\\pdf\\用户信息.pdf", null);
}
}
最终实现效果