|
|
@@ -0,0 +1,59 @@
|
|
|
+package com.common.workflow.service.util;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+
|
|
|
+import com.aspose.cells.License;
|
|
|
+import com.aspose.cells.PdfSaveOptions;
|
|
|
+import com.aspose.cells.SaveFormat;
|
|
|
+import com.aspose.cells.Workbook;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by gyue on 2019-01-21.
|
|
|
+ */
|
|
|
+public class ExcelToPdf {
|
|
|
+
|
|
|
+ public static boolean getLicense() {
|
|
|
+ boolean result = false;
|
|
|
+ try {
|
|
|
+ InputStream is = ExcelToPdf.class.getClassLoader().getResourceAsStream("license.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, HttpServletResponse response) {
|
|
|
+ if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ URL url = new URL(Address);
|
|
|
+ HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
|
|
+ //得到输入流
|
|
|
+ InputStream inputStream = conn.getInputStream();
|
|
|
+ Workbook wb = new Workbook(inputStream);// 原始excel路径
|
|
|
+
|
|
|
+ PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
|
|
|
+ pdfSaveOptions.setOnePagePerSheet(true);
|
|
|
+ pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
|
|
|
+ response.setContentType("application/pdf");
|
|
|
+ OutputStream output = response.getOutputStream();
|
|
|
+ wb.save(output, pdfSaveOptions);
|
|
|
+ output.flush();
|
|
|
+ inputStream.close();
|
|
|
+ output.close();
|
|
|
+ }catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|