09-12
16
用jacob将word文档转换成html文件及常见错误
作者:Java伴侣 日期:2009-12-16
推荐jacob 就是 JAVA-COM Bridge的缩写,它是在java与微软的com组件之间的桥梁,通过使用jacob自带的dll动态链接库通过JNI的方式实现了在sun java平台上的程序对com调用。jacob的作者照搬了微软java开发包中调用com组件的模式,并将它在sun java平台上实现,可谓是用心良苦啊。
这是一个开始于1999年的开源项目的成果,
首先,我们要向jacob的开发者-Dan Adler致敬,感谢他的开源精神和其团队的伟大贡献!
之后,有很多使用者对该项目进行了修改,做出了自己的贡献,我们同样要致以敬意!
1。下在Java-COM连接桥JACOB(http://danadler.com/jacob/),需要jacob.jar和jacob.dll两个文件就可以了(可以到http://sourceforge.net/project/showfiles.php?group_id=109543&package_id=118368下载)。并把jacob.dll放到system32文件夹里面,把jacob.jar放到CLASSPATH里:
注意,不要使用它里面提供的jacob.dll。
jacob.dll可从yahoo的jacob讨论组下载
因为使用它自带的jacob.dll会出现很多错误
2。调用程序(WorkToHtml.java)如下:
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class WorkToHtml{
public static void main(String[] args) throws Exception {
ActiveXComponent app = new ActiveXComponent("Word.Application");// 查找word组件
String inFile = "E:/WWW_HTTP.doc";// 要转换的word文件
String tpFile = "E:/WWW_HTTP.html";// 目标文件
boolean flag = false;
try {
app.setProperty("Visible", new Variant(false));// 设置word不可见
Object docs = app.getProperty("Documents").toDispatch();
Object doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { inFile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();// 打开word文件,注意这里第三个参数要设为true,这个参数表示是否以只读方式打开,因为我们不用保存原文件,所以以只读方式打开,如果你想进行读写,那么就得设为false。
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
tpFile, new Variant(8) }, new int[1]);// 作为html格式保存到目标文件
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f); //关闭word文件
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
}
}
3。编译执行(确保jacob.dll在PATH中或放在system32文件夹里面)
jacob常出现的几种问题:
1、找不到jacob.dll,配置路径问题(确认jacob.dll配置正确,最好将jacob.dll文件放到system32文件夹里)
Exception in thread "……" java.lang.UnsatisfiedLinkError: no jacob in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:992)
at com.jacob.com.Dispatch.<clinit>(Dispatch.java:36)
……
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
------------
2、找不到资源文件,资源文件路径问题(查看程序中资源文件路径的设置,并检查资源文件的存放路径)
Exception in thread "……" com.jacob.com.ComFailException: Invoke of: Open
Source: Microsoft Word
Description: RCHITECTURE=x86
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:890)
at com.jacob.com.Dispatch.callN(Dispatch.java:526)
at com.jacob.com.Dispatch.call(Dispatch.java:606)
at WordControl.openDocument(Test06_08_01.java:3027)
……
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
------------
3、jacob.dll或jacob.jar对应版本错误(可以从yahoo的jacob讨论组重新下载)
com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Documents
Description: An unknown COM error has occured.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent.java)
at com.perlong.poa.common.dao.sqlserver.WordToHtml.change(WordToHtml.java:73)
at com.perlong.poa.common.dao.sqlserver.WordToHtml.main(WordToHtml.java:121)
com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Quit
Description: An unknown COM error has occured.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.invoke(ActiveXComponent.java)
at com.perlong.poa.common.dao.sqlserver.WordToHtml.change(WordToHtml.java:92)
at com.perlong.poa.common.dao.sqlserver.WordToHtml.main(WordToHtml.java:121)
Exception in thread "main"
来源:http://hi.baidu.com/csw_java/blog/item/d1366e547f7c2954564e00e1.html
这是一个开始于1999年的开源项目的成果,
首先,我们要向jacob的开发者-Dan Adler致敬,感谢他的开源精神和其团队的伟大贡献!
之后,有很多使用者对该项目进行了修改,做出了自己的贡献,我们同样要致以敬意!
1。下在Java-COM连接桥JACOB(http://danadler.com/jacob/),需要jacob.jar和jacob.dll两个文件就可以了(可以到http://sourceforge.net/project/showfiles.php?group_id=109543&package_id=118368下载)。并把jacob.dll放到system32文件夹里面,把jacob.jar放到CLASSPATH里:
注意,不要使用它里面提供的jacob.dll。
jacob.dll可从yahoo的jacob讨论组下载
因为使用它自带的jacob.dll会出现很多错误
2。调用程序(WorkToHtml.java)如下:
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class WorkToHtml{
public static void main(String[] args) throws Exception {
ActiveXComponent app = new ActiveXComponent("Word.Application");// 查找word组件
String inFile = "E:/WWW_HTTP.doc";// 要转换的word文件
String tpFile = "E:/WWW_HTTP.html";// 目标文件
boolean flag = false;
try {
app.setProperty("Visible", new Variant(false));// 设置word不可见
Object docs = app.getProperty("Documents").toDispatch();
Object doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { inFile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();// 打开word文件,注意这里第三个参数要设为true,这个参数表示是否以只读方式打开,因为我们不用保存原文件,所以以只读方式打开,如果你想进行读写,那么就得设为false。
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
tpFile, new Variant(8) }, new int[1]);// 作为html格式保存到目标文件
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f); //关闭word文件
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
}
}
3。编译执行(确保jacob.dll在PATH中或放在system32文件夹里面)
jacob常出现的几种问题:
1、找不到jacob.dll,配置路径问题(确认jacob.dll配置正确,最好将jacob.dll文件放到system32文件夹里)
Exception in thread "……" java.lang.UnsatisfiedLinkError: no jacob in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:992)
at com.jacob.com.Dispatch.<clinit>(Dispatch.java:36)
……
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
------------
2、找不到资源文件,资源文件路径问题(查看程序中资源文件路径的设置,并检查资源文件的存放路径)
Exception in thread "……" com.jacob.com.ComFailException: Invoke of: Open
Source: Microsoft Word
Description: RCHITECTURE=x86
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:890)
at com.jacob.com.Dispatch.callN(Dispatch.java:526)
at com.jacob.com.Dispatch.call(Dispatch.java:606)
at WordControl.openDocument(Test06_08_01.java:3027)
……
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
------------
3、jacob.dll或jacob.jar对应版本错误(可以从yahoo的jacob讨论组重新下载)
com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Documents
Description: An unknown COM error has occured.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent.java)
at com.perlong.poa.common.dao.sqlserver.WordToHtml.change(WordToHtml.java:73)
at com.perlong.poa.common.dao.sqlserver.WordToHtml.main(WordToHtml.java:121)
com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Quit
Description: An unknown COM error has occured.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.invoke(ActiveXComponent.java)
at com.perlong.poa.common.dao.sqlserver.WordToHtml.change(WordToHtml.java:92)
at com.perlong.poa.common.dao.sqlserver.WordToHtml.main(WordToHtml.java:121)
Exception in thread "main"
来源:http://hi.baidu.com/csw_java/blog/item/d1366e547f7c2954564e00e1.html
评论: 0 | 引用: 0 | 查看次数: 598
发表评论