From c41f8c8a75c4475831e3c7e9cde7467d2fb39950 Mon Sep 17 00:00:00 2001 From: jiangping <jp@doumee.com> Date: 星期五, 28 六月 2024 10:38:17 +0800 Subject: [PATCH] 提交一把 --- server/service/src/main/java/com/doumee/core/annotation/excel/ExcelToPdfToolNew.java | 106 ++++++++++++++++++++++++++-------------------------- 1 files changed, 53 insertions(+), 53 deletions(-) diff --git a/server/service/src/main/java/com/doumee/core/annotation/excel/ExcelToPdfToolNew.java b/server/service/src/main/java/com/doumee/core/annotation/excel/ExcelToPdfToolNew.java index b1d574c..b865a52 100644 --- a/server/service/src/main/java/com/doumee/core/annotation/excel/ExcelToPdfToolNew.java +++ b/server/service/src/main/java/com/doumee/core/annotation/excel/ExcelToPdfToolNew.java @@ -8,43 +8,43 @@ public class ExcelToPdfToolNew { - /** - * 浣跨敤jacob瀹炵幇excel杞琍DF - * - */ + /** + * 浣跨敤jacob瀹炵幇excel杞琍DF + * + */ public static void excelToPdf(String inputFilePath, String outputFilePath) { - ActiveXComponent ax = null; - Dispatch excel = null; - try { - ComThread.InitSTA(); - ax = new ActiveXComponent("Excel.Application"); - ax.setProperty("Visible", new Variant(false)); - // 绂佺敤瀹� - ax.setProperty("AutomationSecurity", new Variant(3)); - Dispatch excels = ax.getProperty("Workbooks").toDispatch(); - Object[] obj = { - inputFilePath, - new Variant(false), - new Variant(false) - }; - excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch(); - Dispatch currentSheet = Dispatch.get(excel, - "ActiveSheet").toDispatch(); - // 鑾峰彇椤甸潰璁剧疆 - Dispatch pageSetup = Dispatch.get(currentSheet, "PageSetup") .toDispatch(); - Dispatch.put(pageSetup, "Orientation", new Variant(2)); - Dispatch.put(pageSetup, "FitToPagesTall", new Variant(false)); - Dispatch.put(pageSetup, "FitToPagesWide", new Variant(false)); - // 鑾峰彇椤甸潰璁剧疆 + ActiveXComponent ax = null; + Dispatch excel = null; + try { + ComThread.InitSTA(); + ax = new ActiveXComponent("Excel.Application"); + ax.setProperty("Visible", new Variant(false)); + // 绂佺敤瀹� + ax.setProperty("AutomationSecurity", new Variant(3)); + Dispatch excels = ax.getProperty("Workbooks").toDispatch(); + Object[] obj = { + inputFilePath, + new Variant(false), + new Variant(false) + }; + excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch(); + Dispatch currentSheet = Dispatch.get(excel, + "ActiveSheet").toDispatch(); + // 鑾峰彇椤甸潰璁剧疆 + Dispatch pageSetup = Dispatch.get(currentSheet, "PageSetup") .toDispatch(); + Dispatch.put(pageSetup, "Orientation", new Variant(2)); + Dispatch.put(pageSetup, "FitToPagesTall", new Variant(0)); + Dispatch.put(pageSetup, "FitToPagesWide", new Variant(1)); + // 鑾峰彇椤甸潰璁剧疆 // Dispatch.put(pageSetup, "PaperSize", pageSize); - // 鍐呭缂╂斁 + // 鍐呭缂╂斁 // Dispatch.put(pageSetup, "Zoom", new Variant(false)); - // Dispatch Application = Dispatch.get(currentSheet, "Application") .toDispatch(); + // Dispatch Application = Dispatch.get(currentSheet, "Application") .toDispatch(); // Dispatch.put(pageSetup, "PaperSize", pageSize); // Dispatch.put(Application, "ActivePrinter", "Microsoft Print to PDF"); - // 鍐呭缂╂斁 + // 鍐呭缂╂斁 // Dispatch.put(pageSetup, "Orientation", new Variant(2)); // Dispatch.put(pageSetup, "Zoom", new Variant(false)); // Dispatch.put(pageSetup, "FitToPagesWide", new Variant(1)); @@ -52,33 +52,33 @@ // Dispatch.put(pageSetup, "PaperSize", new Variant(8)); // A3鏄�8锛孉4鏄�9锛孉5鏄�11绛夌瓑 - // 杞崲鏍煎紡 - Object[] obj2 = { - // PDF鏍煎紡绛変簬0 - new Variant(0), - outputFilePath, - // 0=鏍囧噯锛堢敓鎴愮殑PDF鍥剧墖涓嶄細妯$硦锛夛紝1=鏈�灏忕殑鏂囦欢 - new Variant(0) - }; - Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, obj2, new int[1]); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { - if (excel != null) { - Dispatch.call(excel, "Close", new Variant(false)); - } - if (ax != null) { - ax.invoke("Quit", new Variant[]{}); - ax = null; - } - ComThread.Release(); + // 杞崲鏍煎紡 + Object[] obj2 = { + // PDF鏍煎紡绛変簬0 + new Variant(0), + outputFilePath, + // 0=鏍囧噯锛堢敓鎴愮殑PDF鍥剧墖涓嶄細妯$硦锛夛紝1=鏈�灏忕殑鏂囦欢 + new Variant(0) + }; + Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, obj2, new int[1]); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } finally { + if (excel != null) { + Dispatch.call(excel, "Close", new Variant(false)); } + if (ax != null) { + ax.invoke("Quit", new Variant[]{}); + ax = null; + } + ComThread.Release(); } + } public static void main(String[] args) { - String inputExcelPath = "D:\\3.xlsx"; - String outputPdfPath = "D:\\33.pdf"; + String inputExcelPath = "D:\\4.xlsx"; + String outputPdfPath = "D:\\44.pdf"; excelToPdf(inputExcelPath, outputPdfPath); } -- Gitblit v1.9.3