k94314517
2025-07-01 f7a3d41075e00498f7033475108c0bcd86cda733
server/service/src/main/java/com/doumee/core/annotation/excel/ExcelToPdfToolNew.java
@@ -8,43 +8,43 @@
public class ExcelToPdfToolNew {
        /**
         * 使用jacob实现excel转PDF
         *
         */
    /**
     * 使用jacob实现excel转PDF
     *
     */
    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,A4是9,A5是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);
    }