wps doc

/**
     * word转PDF
     *
     * @param inFilePath  输入文件全路径
     * @param outFilePath 输出文件全路径
     */
    public static void wordToPdf(String inFilePath, String outFilePath) {
        System.out.println("inFilePath = " + inFilePath);
        System.out.println("outFilePath = " + outFilePath);
        Long start = System.currentTimeMillis();
        //构建ActiveX组件实例
        ActiveXComponent axc = null;
        //初始化Dispatch对象
        Dispatch word = null;
        try {
            //初始化com线程(不初始化会出现异常)
            ComThread.InitSTA();
            /*
                //操作Microsoft Office
                axc = new ActiveXComponent("Word.Application");
            */
            //操作WPS Office
            axc = new ActiveXComponent("KWPS.Application");
            //设置应用操作不开启窗口,在后台静默处理
            axc.setProperty("Visible", new Variant(false));
            //获得该ActiveX控件的控制权
            word = axc.getProperty("Documents").toDispatch();
            //方法参数包装(具体的去看巨硬或者WPS的VBA文档)
            Object[] params1 = {
                    inFilePath,//文档名(可包含路径。我这里是方法直接接收的全路径)
                    new Variant(false),//如果该属性为 True,则当文件不是word格式时,将显示“文件转换”对话框
                    new Variant(true)//如果该属性值为 True,则以只读方式打开文档。该参数不会覆盖保存的文档的只读建议设置。例如,如果文档在只读建议启用的情况下保存,则将 ReadOnly 参数设置为 False 不会导致文件以可读写方式打开
            };
            word = Dispatch.invoke(word, "Open", Dispatch.Method, params1, new int[1]).toDispatch();
            //创建一个文件对象
            File file = new File(outFilePath);
            //检查磁盘中是否已经存在该文件,若存在就删除
            if (file.exists()) {
                boolean delete = file.delete();
            }
            Object[] params2 = {
                    outFilePath,//新的 PDF 或 XPS 文件的路径和文件名
                    new Variant(17)//指定采用 PDF:17 格式或 XPS:18 格式
            };
            Dispatch.invoke(word, "ExportAsFixedFormat", Dispatch.Method, params2, new int[1]);
            Long end = System.currentTimeMillis();
            System.out.println("文档转换成功·用时:" + (end - start) + "ms.");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("文档转换失败:" + e.getMessage());
        } finally {
            //关闭Word
            if (word != null) {
                Dispatch.call(word, "Close", false);
            }
            if (axc != null) {
                axc.invoke("Quit", new Variant[]{});
            }
            //关闭com线程(不关闭会损耗系统性能)
            ComThread.Release();
        }
    }

/**
     * excel转PDF
     *
     * @param inFilePath  输入文件全路径
     * @param outFilePath 输出文件全路径
     */
    public static void excelToPdf(String inFilePath, String outFilePath) {
        System.out.println("inFilePath = " + inFilePath);
        System.out.println("outFilePath = " + outFilePath);
        Long start = System.currentTimeMillis();
        ActiveXComponent axc = null;
        Dispatch excel = null;
        try {
            ComThread.InitSTA();
            //axc = new ActiveXComponent("Excel.Application");
            axc = new ActiveXComponent("KET.Application");
            axc.setProperty("Visible", new Variant(false));
            //禁用宏
            axc.setProperty("AutomationSecurity", new Variant(3));
            //打开Excel文件
            excel = axc.getProperty("Workbooks").toDispatch();
            Object[] params1 = new Object[]{
                    inFilePath,
                    new Variant(false),
                    new Variant(false)//如果为 True,则以只读模式打开工作簿
            };
            excel = Dispatch.invoke(excel, "Open", Dispatch.Method, params1, new int[9])
                    .toDispatch();
            //创建一个文件对象
            File file = new File(outFilePath);
            //检查磁盘中是否已经存在该文件,若存在就删除
            if (file.exists()) {
                boolean delete = file.delete();
            }
            //转换格式
            Object[] params2 = new Object[]{
                    new Variant(0),//指定采用 PDF:0 格式或 XPS:1 格式
                    outFilePath,
                    new Variant(0) //指定采用 标准:0(图片不会变模糊) 质量 或者 压缩:1(可以减小文件体积) 质量
            };
            Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, params2, new int[1]);
            Long end = System.currentTimeMillis();
            System.out.println("文档转换成功·用时:" + (end - start) + "ms.");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("文档转换失败:" + e.getMessage());
        } finally {
            //关闭Excel
            if (excel != null) {
                Dispatch.call(excel, "CLose", new Variant(false));
            }
            if (axc != null) {
                axc.invoke("Quit", new Variant[]{});
            }
            ComThread.Release();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值