Java 将 Excel 转 PDF
639人浏览 / 0人评论
参考:
https://blog.csdn.net/weixin_43907947/article/details/118992070
Excel2PdfUtil.java
import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
public class Excel2PdfUtil {
/**
* excel文件导出为PDF文件
* @param Address 需要转化的Excel文件地址,
*
*/
public static void excel2pdf(String Address) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
try {
File pdfFile = new File("src/main/resources/template/肩关节功能评定量表.pdf"); // 输出路径
FileInputStream excelstream = new FileInputStream(Address);
Workbook wb = new Workbook(excelstream);// excel路径,这里是先把数据放进缓存表里,然后把缓存表转化成PDF
FileOutputStream fileOS = new FileOutputStream(pdfFile);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);//参数true把内容放在一张PDF页面上;
wb.save(fileOS, pdfSaveOptions);
fileOS.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//获取认证,去除水印
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = Excel2PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");//这个文件应该是类似于密码验证(证书?),用于获得去除水印的权限
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
jar包下载:http://pan.jbritian.com/share/6db381e5c117483abbe80504ca9684d9
license.xml(放在resource目录下):
<?xml version="1.0" encoding="UTF-8"?>
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Excel 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>
全部评论