`
阿杜杜不是阿木木
  • 浏览: 20353 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

jacob将Office文件转换为html文件工具类

 
阅读更多

更新日期:20150902

package com.tools.util;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class Jacob2Html{
	
 	/**  
     * PowerPoint转成HTML  
     * @param pptPath PowerPoint文件全路径  
     * @param htmlfile 转换后HTML存放路径  
    */  
    public static void pptToHtml(String pptPath, String htmlPath)   
    {   
     ActiveXComponent offCom = new ActiveXComponent("PowerPoint.Application"); 
        try  
        {   
         offCom.setProperty("Visible", new Variant(true));   
            Dispatch excels = offCom.getProperty("Presentations").toDispatch();   
            Dispatch excel = Dispatch.invoke(excels, 
             "Open",Dispatch.Method, new Object[] { pptPath, new Variant(false),  new Variant(false) }, new int[1]).toDispatch();   
            Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {   
              htmlPath, new Variant(12) }, new int[1]);   
            // Variant f = new Variant(false);   
            Dispatch.call(excel, "Close");   
        }   
        catch (Exception e)   
        {   
            e.printStackTrace();   
        }   
        finally  
        {   
         offCom.invoke("Quit", new Variant[] {});   
        }   
    }
      
    /**  
     * WORD转成HTML  
     * @param wordPath WORD文件全路径  
     * @param htmlPath 生成的HTML存放路径  
     */  
    public static void wordToHtml(String wordPath, String htmlPath)   
    { 
     ActiveXComponent offCom = new ActiveXComponent("Word.Application"); 
        try  
        {   
         offCom.setProperty("Visible", new Variant(false));   
            Dispatch wordDis = offCom.getProperty("Documents").toDispatch();   
            Dispatch doc = Dispatch.invoke(wordDis,   
                    "Open", Dispatch.Method,  new Object[] { wordPath, new Variant(false),   
                            new Variant(true) }, new int[1]).toDispatch();   
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {   
              htmlPath, new Variant(8) }, new int[1]);   
            Variant f = new Variant(false);   
            Dispatch.call(doc, "Close", f);   
        }   
        catch (Exception e)   
        {   
            e.printStackTrace();   
        }   
        finally  
        {   
         offCom.invoke("Quit", new Variant[] {});   
        }   
    }   
  
    /**  
     * EXCEL转成HTML  
     * @param xlsfile EXCEL文件全路径  
     * @param htmlfile 转换后HTML存放路径  
     */  
    public static void excelToHtml(String excelPath, String htmlPath)   
    {   
        ActiveXComponent offCom = new ActiveXComponent("Excel.Application"); 
        try  
        {   
         offCom.setProperty("Visible", new Variant(false));   
            Dispatch excels = offCom.getProperty("Workbooks").toDispatch();   
            Dispatch excel = Dispatch.invoke(excels, 
             "Open",Dispatch.Method, new Object[] { excelPath, new Variant(false),  new Variant(true) }, new int[1]).toDispatch();   
            Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {   
              htmlPath, new Variant(44) }, new int[1]);   
            Variant f = new Variant(false);   
            Dispatch.call(excel, "Close", f);   
        }   
        catch (Exception e)   
        {   
            e.printStackTrace();   
        }   
        finally  
        {   
         offCom.invoke("Quit", new Variant[] {});   
        }   
    }    
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics