利用 aspose-cells-8.5.2.jar 实现 excel 内容转换为 pdf 文件输出。
开发过程中如果不验证 License 输出文件中会产生水印,详见如下 verifyLicense() 方法
主要实现代码:
/**
* @param src 需要被转换的excel全路径带文件名
* @param dest 转换之后pdf的全路径带文件名
* @throws GeneralException
*/
public static void convert(String src,String dest) throws GeneralException {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!verifyLicense()) {
throw new GeneralException("License验证失败!");
}
try (FileOutputStream fileOS = new FileOutputStream(new File(dest));) {
Workbook wb = new Workbook(src);
wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}
}
private static boolean verifyLicense() {
boolean result = false;
try {
InputStream is = Excel2pdfUtil.class.getResourceAsStream("/license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
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>