package com.demo; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import com.aspose.cells.License; //引入aspose-cells-8.5.2.jar包 import com.aspose.cells.SaveFormat; import com.aspose.cells.Workbook; /** * @version 1.0 说明 : */ public class Excel2Pdf { public static void main(String[] args) { String sourceFilePath="L:/testPDF/test.xls";//可生成PDF 没问题 String desFilePath="L:\\testPDF\\PDF\\test.pdf"; excel2pdf(sourceFilePath, desFilePath); } public static boolean getLicense() { boolean result = false; try { InputStream is = Word2Pdf.class.getClassLoader() .getResourceAsStream("\\license.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public static void excel2pdf(String sourceFilePath, String desFilePath) { if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生 return; } try { File pdfFile = new File(desFilePath);// 输出路径 Workbook wb = new Workbook(sourceFilePath);// 原始excel路径 FileOutputStream fileOS = new FileOutputStream(pdfFile); wb.save(fileOS, SaveFormat.PDF); fileOS.close(); } catch (Exception e) { e.printStackTrace(); } } }
最近下载更多