java开发的项目有一个需求,Excel转pdf,现有一种方式解决,方法如下。
1.引入aspose依赖
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
</dependency>
2.创建一个license.xml文件
创建一个license,在转换时加载破解,license.xml放在java项目的resource目录下。
<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>
3.代码示例
java代码转换示例。
public class Test {
/**
* 获取license
*
* @return
*/
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = Test.class.getClassLoader().getResourceAsStream("\license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 支持DOC, DOCX, OOXML, RTF, HTML, OpenDocument, PDF, EPUB, XPS, SWF等相互转换<br>
*
* @param args
*/
public static void main(String[] args) {
// 验证License
if (!getLicense()) {
return;
}
try {
long old = System.currentTimeMillis();
String inputFilePath = "D:\excel\20231121165826.xlsx";
String outputFilePath = "D:\excel\zdf.pdf";
Workbook workbook = new Workbook(inputFilePath);
// 创建PdfSaveOptions对象
PdfSaveOptions options = new PdfSaveOptions();
// 设置内容过宽时自动调整列宽
options.setAllColumnsInOnePagePerSheet(true);
// 将Excel文件转换为PDF并保存
workbook.save(outputFilePath, options);
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
}
}
上一条:Docker-Elasticsearch 安装的详细指南
下一条:Aspose软件介绍