zoukankan      html  css  js  c++  java
  • Java中Office(word/ppt/excel)转换成HTML实现

    运行条件:JDK + jacob.jar + jacob.dll

    1) 把jacob.dllJAVA_HOMEin 和 JAVA_HOMEjrein 以及C:WINDOWSsystem32目录下各放一份
    2) 把jacob.jar放入 项目的lib包下,并且在“java构建路径”中也要加载此jar包。
    3) 运行项目即可编译通过.

    注:jacob.jar以及jacob.dll版本要和jdk版本相匹配,否则可能出现异常!

    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.*;
     
    public class OfficeToHTML {
     
        private final static OfficeToHTML oOfficeToHTML = new OfficeToHTML();
     
        public static OfficeToHTML getInstance() {
            return oOfficeToHTML;
        }
     
        public OfficeToHTML() {
        }
     
        public boolean WordtoHtml(String s, String s1) {
            ComThread.InitSTA();
            ActiveXComponent activexcomponent = new ActiveXComponent(
                    "Word.Application");
            String s2 = s;
            String s3 = s1;
            boolean flag = false;
            try {
                activexcomponent.setProperty("Visible", new Variant(false));
                Dispatch dispatch = activexcomponent.getProperty("Documents").toDispatch();
                Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open", 1,
                        new Object[] { s2, new Variant(false), new Variant(true) },
                        new int[1]).toDispatch();
                Dispatch.invoke(dispatch1, "SaveAs", 1, new Object[] { s3,
                        new Variant(8) }, new int[1]);
                Variant variant = new Variant(false);
                Dispatch.call(dispatch1, "Close", variant);
                flag = true;
            } catch (Exception exception) {
                exception.printStackTrace();
            } finally {
                activexcomponent.invoke("Quit", new Variant[0]);
                ComThread.Release();
                ComThread.quitMainSTA();
            }
            return flag;
        }
     
        public boolean PPttoHtml(String s, String s1) {
            ComThread.InitSTA();
            ActiveXComponent activexcomponent = new ActiveXComponent(
                    "PowerPoint.Application");
            String s2 = s;
            String s3 = s1;
            boolean flag = false;
            try {
                Dispatch dispatch = activexcomponent.getProperty("Presentations")
                        .toDispatch();
                Dispatch dispatch1 = Dispatch.call(dispatch, "Open", s2,
                        new Variant(-1), new Variant(-1), new Variant(0))
                        .toDispatch();
                Dispatch.call(dispatch1, "SaveAs", s3, new Variant(12));
                Variant variant = new Variant(-1);
                Dispatch.call(dispatch1, "Close");
                flag = true;
            } catch (Exception exception) {
                System.out.println("|||" + exception.toString());
            } finally {
                activexcomponent.invoke("Quit", new Variant[0]);
                ComThread.Release();
                ComThread.quitMainSTA();
            }
            return flag;
        }
     
        public boolean ExceltoHtml(String s, String s1) {
             ComThread.InitSTA();
             ActiveXComponent activexcomponent = new
             ActiveXComponent("Excel.Application");
             String s2 = s;
             String s3 = s1;
             boolean flag = false;
             try
             {
             activexcomponent.setProperty("Visible", new Variant(false));
             Dispatch dispatch =
             activexcomponent.getProperty("Workbooks").toDispatch();
             Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open", 1, new
             Object[] {
             s2, new Variant(false), new Variant(true)
             }, new int[1]).toDispatch();
             Dispatch.call(dispatch1, "SaveAs", s3, new Variant(44));
             Variant variant = new Variant(false);
             Dispatch.call(dispatch1, "Close", variant);
             flag = true;
             }
             catch(Exception exception)
             {
             System.out.println("|||" + exception.toString());
             }
             finally
             {
             activexcomponent.invoke("Quit", new Variant[0]);
             ComThread.Release();
             ComThread.quitMainSTA();
             }
             return flag;
        }
     
        public static void main(String args[]) {
            OfficeToHTML otx = OfficeToHTML.getInstance();
            boolean flag1 = otx.PPttoHtml("e:/test/test3.pptx", "e:/test/test3.html");
            if(flag1){
                System.out.println("PPT文件转换成HTML成功!");
            }else{
                System.out.println("PPT文件转换成HTML失败!");
            }
        }
    }
  • 相关阅读:
    session之验证码
    session之cookie封装终极版本
    临床知识库-临床路径管理
    import org.quartz.Job; 不存在
    ASP.NET DataGrid 导出EXCEL 中文变乱码
    Cannot find module '@/views/monitor/online/index'
    配置包导入
    配置log4j 以便查看mybatis操作数据库的过程
    解决中文乱码问题
    ORA 环境变量
  • 原文地址:https://www.cnblogs.com/yzuzhang/p/5134246.html
Copyright © 2011-2022 走看看