下載aspose-cells-8.5.2.jar包 http://pan.baidu.com/s/1kUBzsQ7
JAVA代碼
package webViewer; import java.io.*; import com.aspose.cells.*; //引入aspose-cells-8.5.2.jar包 public class Excel2Pdf { public static boolean getLicense() { boolean result = false; try { InputStream is = Test.class.getClassLoader().getResourceAsStream("xlsxlicense.xml"); // license.xml應放在..\WebRoot\WEB-INF\classes路徑下 License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public static void excel2pdf(String Address) { if (!getLicense()) { // 驗證License 若不驗證則轉化出的pdf文檔會有水印產生 return; } try { File pdfFile = new File("C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/webapps/generic/web/file/pdf1.pdf");// 輸出路徑 Workbook wb = new Workbook(Address);// 原始excel路徑 FileOutputStream fileOS = new FileOutputStream(pdfFile); wb.save(fileOS, SaveFormat.PDF); fileOS.close(); } catch (Exception e) { e.printStackTrace(); } } }
調用代碼
package webViewer; public class Test { public static void main(String[] args){ Excel2Pdf.excel2pdf("C:/Program Files (x86)/Apache Software Foundation/Tomcat 7.0/webapps/generic/web/file/5.xlsx"); } }
生成pdf文檔