直接贴Excel转PDF方法
/**
excelFileDir:excel文件路径
excelFileName:excel文件名称(包含扩展名,不包含下面默认给.xls)
*/
public String convertExcelToPdf(String excelFileDir, String excelFileName) throws Exception {
String pdfFileName = "";//声明pdf文件名称
try {
//判断Excel文件名称是否为空,为空直接返回
if (StringUtility.isNullOrEmpty(excelFileName)) {
return "";
}
// 没xls后缀,则添加
excelFileName = excelFileName.indexOf(".") == -1 ? excelFileName + ".xls" : excelFileName;
// pdf文件名设置
pdfFileName = excelFileName.toLowerCase().replace(".xls", ".pdf").replace(".xlsx", ".pdf");
String excelFilePath = excelFileDir + excelFileName;
//设置pdf文件输出路径和Excel文件路径一致
String pdfFilePath = excelFileDir + pdfFileName;
//判断Excel文件是否存在
if (new File(excelFilePath).exists()) {
// xls转pdf
Workbook workbook = new Workbook(excelFilePath);
File pdfFile = new File(pdfFilePath);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
FileOutputStream fileOutputStream = new FileOutputStream(pdfFile);
workbook.save(fileOutputStream, SaveFormat.PDF);
} else {
return "";
}
} catch (Exception e) {
logger.error(e.getMessage());
}
return pdfFileName;
}
其中所需引用包:
java.io.File;
java.io.FileOutputStream
com.aspose.cells.Workbook;
com.aspose.cells.PdfSaveOptions;