在实际项目开发中,通常需要实现excel文件转pdf文件功能,以便于用户能够更好的对文件进行打印和归档。要做好这个功能需要解决以下两个难点问题:
转成后带水印的pdf文件
下面将采用第三方java包aspose-cells-8.5.2.jar完美解决以上两个问题,具体步骤如下:
1.集成aspose-cells-8.5.2.jar
下载jar包后在项目目录下建一个lib目录,将jar包拷贝其中。如下图所示:
集成aspose-cells-8.5.2.jar包
2.配置pom文件
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aspose-cells-8.5.2.jar</systemPath>
</dependency>
3.下载aspose授权文件
下载aspose授权文件,放在如下图目录下:
授权文件存放目录
license.xml授权文件内容如下:
<License>
<Data>
<Products>
<Product>Aspose.Total 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.编写excel转pdf工具类?
4.编写excel转pdf程序
package com.hellxz.rabbitmq.ssl;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class PdfAsposeUtil {
public static boolean getLicenseExcel() {
boolean result = false;
InputStream is = null;
try {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:license.xml");
is = resources[0].getInputStream();
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* excel转pdf
*
* @param inpath excel文件地址
* @param outPath pdf地址
*/
public static boolean excel2pdf(String inpath, String outPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicenseExcel()) {
return false;
}
FileOutputStream outputStream = null;
try {
long old = System.currentTimeMillis();
File file = new File(outPath); // 新建一个空白pdf文档
outputStream = new FileOutputStream(file);
Workbook wb = new Workbook(inpath);// 原始excel路径
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
//当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放,不分页。
pdfSaveOptions.setOnePagePerSheet(true);
wb.save(outputStream, pdfSaveOptions);
long now = System.currentTimeMillis();
System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
return true;
}
}
以上是在java中实现excel转pdf文件的详细实现方案。