Java-jacob-Excel/Word转PDF

jacob下载:https://sourceforge.net/projects/jacob-project/files/

我调用的是MicroSoft Office,使用WPS请根据实际情况修改

Excel转PDF(可以将所有sheet全部输出为一个PDF):

问题:如果excel设置了页码,转为PDF后页码显示不正确,有能力的自己找找原因吧

    /**
     * excel转PDF
     *
     * @param inFilePath  输入文件全路径
     * @param outFilePath 输出文件全路径
     */
    public static void excelToPdf(String inFilePath, String outFilePath) {
        System.out.println("启动Excel...");
        Long start = System.currentTimeMillis();
        ActiveXComponent axc = null;
        Dispatch excel = null;
        try {
            ComThread.InitSTA();
            axc = new ActiveXComponent("Excel.Application");
            axc.setProperty("Visible", new Variant(false));
            axc.setProperty("AutomationSecurity", new Variant(3));//禁用宏
            //打开Excel文件
            Dispatch excels = axc.getProperty("Workbooks").toDispatch();
            Object[] obj = new Object[]{
                    inFilePath,
                    new Variant(false),
                    new Variant(false)
            };
            excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9])
                    .toDispatch();
            System.out.println("打开文档..." + inFilePath);
            System.out.println("转换文档到PDF..." + outFilePath);
            File tofile = new File(outFilePath);
            //文件保存到磁盘
            if (tofile.exists()) {
                boolean delete = tofile.delete();
            }
            //转换格式
            Object[] obj2 = new Object[]{
                    new Variant(0),//PDF格式=0
                    outFilePath,
                    new Variant(0) //0=标准 (生成的PDF图片不会变的模糊) ; 1=最小文件
            };
            Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, obj2, new int[1]);
            Long end = System.currentTimeMillis();
            System.out.println("转换完成...用时:" + (end - start) + "ms.");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error:文档转换失败:" + e.getMessage());
        } finally {
            //关闭Excel
            if (excel != null) {
                Dispatch.call(excel, "CLose", new Variant(false));
            }
            System.out.println("关闭文档");
            if (axc != null) {
                axc.invoke("Quit", new Variant[]{});
            }
            ComThread.Release();
        }
    }

Word转PDF:

    /**
     * word转成为PDF
     *
     * @param inFilePath  输入文件全路径
     * @param outFilePath 输出文件全路径
     */
    public static void wordToPdf(String inFilePath, String outFilePath) {
        System.out.println("启动Word...");
        Long start = System.currentTimeMillis();
        ActiveXComponent axc = null;
        Dispatch word = null;
        try {
            ComThread.InitSTA();
            axc = new ActiveXComponent("Word.Application");
            axc.setProperty("Visible", new Variant(false));
            //打开Word文件
            Dispatch words = axc.getProperty("Documents").toDispatch();
            Object[] obj = {
                    inFilePath,
                    new Variant(false),
                    new Variant(true)
            };
            word = Dispatch.invoke(words, "Open", Dispatch.Method, obj, new int[1])
                    .toDispatch();
            System.out.println("打开文档..." + inFilePath);
            System.out.println("转换文档到PDF..." + outFilePath);
            File tofile = new File(outFilePath);
            //文件保存到磁盘
            if (tofile.exists()) {
                boolean delete = tofile.delete();
            }
            Object[] obj2 = {
                    outFilePath,
                    new Variant(17)
            };
            Dispatch.invoke(word, "SaveAs", Dispatch.Method, obj2, new int[1]);
            Long end = System.currentTimeMillis();
            System.out.println("转换完成...用时:" + (end - start) + "ms.");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error:文档转换失败:" + e.getMessage());
        } finally {
            //关闭Word
            if (word != null) {
                Dispatch.call(word, "Close", false);
            }
            System.out.println("关闭文档");
            if (axc != null) {
                axc.invoke("Quit", new Variant[]{});
            }
            //如果没有这句话winword.exe进程将不会关闭
            ComThread.Release();
        }
    }

猜你喜欢

转载自www.cnblogs.com/yqk150/p/13365248.html