关于java使用jacob调用word,ppt,excel等的转化为html的问题

转载 2015年07月07日 10:08:51
使用jacob时,把dll放到window/system32下就成,不需要注册 
1、如果出现下面的错误 
   com.jacob.com.ComFailException: A COM exception has been encountered: 
   At Invoke of: Version 
   Description: An unknown COM error has occured. 
   表示dll的版本不对,换成最新版本即可。 
2、如果出现下面的错误 
   no jacob in java.library.path 
   java.lang.UnsatisfiedLinkError: no jacob in java.library.path 
   表示把dll放到path下即可,设置path或是放到window/system32下

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 flag3 = otx.ExceltoHtml("e:/test/test1.xlsx", "e:/test/test1.html");  
        if(flag3){  
            System.out.println("EXCEL文件转换成HTML成功!");  
        }else{  
            System.out.println("EXCEL文件转换成HTML失败!");  
        }  
        boolean flag2 = otx.WordtoHtml("e:/test/test2.docx", "e:/test/test2.html");  
        if(flag2){  
            System.out.println("WORD文件转换成HTML成功!");  
        }else{  
            System.out.println("WORD文件转换成HTML失败!");  
        }  
        boolean flag1 = otx.PPttoHtml("e:/test/test4.ppt", "e:/test/test4.html");  
        if(flag1){  
            System.out.println("PPT文件转换成HTML成功!");  
        }else{  
            System.out.println("PPT文件转换成HTML失败!");  
        }  
    }  
}  

相关文章推荐

java中各种date格式的转化

/** 日期类 * @date   2003-12-04 * @version 1.0 */import java.util.*;import java.text.*;import java.util...

java jacob 操作word 文档,进行写操作,如生成表格,添加 图片

jacob-1.15-M3.zip  jacob-1.15-M3-x86.dll copy 到c:\\windows\system32 引入jacob.jar 示例代码     imp...

Java 使用jacob ppt文件转pptx,doc转docx;word 转html、pdf等

Java 使用jacob ppt文件转pptx,doc转docx

使用maven管理jacob.jar对excel、word文档进行转换为html细节描述

使用maven管理jacob.jar对excel、word文档进行转换为html细节描述

Jacob 环境配置 + word/excel/ppt 转 PDF

java,Jacob 环境配置 + word/excel/ppt 转 PDF

java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件

最近跟我同事一起做的项目要求读取txt,word,excel,ppt,pdf,html中的内容,不多说,先把代码贴出来,之后有时间再来做详细的解读。 这是读取txt文件 /** * 获取t...

java实现在线预览--poi实现word、excel、ppt转html

java实现在线预览 - -之poi实现word、excel、ppt转html 简介 Java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线...

java(word/ppt/excel)转换成HTML实现

[java] view plaincopyprint? import com.jacob.activeX.ActiveXComponent;   import com....

PDF文件转化成word,ppt,excel,图片(png,jpg...),tiff,rtf,txt,html,PDF组合,PDF编辑,PDF创建

PDF文件转化成word,ppt,excel,图片(png,jpg...),tiff,rtf,txt,html,PDF组合,PDF编辑,PDF创建...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:关于java使用jacob调用word,ppt,excel等的转化为html的问题
举报原因:
原因补充:

(最多只允许输入30个字)