前些天自己学习了一下Jacob的使用,总是遇上一些问题,请大家帮帮我的忙。
先是路径的问题。本人使用的OS是Windows XP,将jacob.jar添加到classpath没问题,将jacob.dll添加到path却出现问题了,Java程序读不出com.jacob.com.*和com.jacob.activeX.*。最后用一个老办法,把jacob.dll直接复制到system32目录下,总算解决了读“包”的问题。
编译又出现问题了。现将代码和出现的错误写在下面,恳请大家指教:
/*-------------------------------------------------------
-------------------------------------------------------*/
import java.io.File;import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/* //取得指定目录下面所有的doc文件名称*/
public class wordtohtml
{
/* //-------------------------------------------------
//方法原型: change(String paths)
//功能描述: 将指定目录下面所有的doc文件转化为HTML并存储在相同目录下
//输入参数: String
//输出参数: 无
//返 回 值: 无
//其它说明: 递归
//-------------------------------------------- */
public static void change(String paths, String savepaths){
File d = new File(paths);
/* //取得当前文件夹下所有文件和目录的列表*/
File lists[] = d.listFiles();
String pathss = new String("");
/* //对当前目录下面所有文件进行检索*/
for(int i = 0; i < lists.length; i ++)
{
if(lists[i].isFile())
{
String filename = lists[i].getName();
String filetype = new String("");
/* //取得文件类型*/
filetype = filename.substring((filename.length() - 3), filename.length());
/* //判断是否为doc文件*/
if(filetype.equals("doc"))
{
System.out.println("当前正在转换......");
/* //打印当前目录路径*/
System.out.println(paths);
/* //打印doc文件名*/
System.out.println(filename.substring(0, (filename.length() - 4)));
ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
String docpath = paths + filename;
String htmlpath = savepaths + filename.substring(0, (filename.length() - 4));
String inFile = docpath;
/* //要转换的word文件*/
String tpFile = htmlpath+".htm";
/* //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文件*/
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);
flag = true;
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
}
System.out.println("转化完毕!");
}
}
else
{
pathss = paths;
/* //进入下一级目录*/
pathss = pathss + lists[i].getName() + "\\";
/* //递归遍历所有目录*/
change(pathss, savepaths);
}
}
}
/* //---------------------------------------------------------
//方法原型: main(String[] args)
//功能描述: main文件
//输入参数: 无
//输出参数: 无
//返 回 值: 无
//其它说明: 无
//---------------------------------------------------------- */
public static void main(String[] args)
{
String paths = new String("D:\\JavaProgram\\wordtohtml\\word\\");
String savepaths = new String ("D:\\JavaProgram\\wordtohtml\\html\\");
change(paths, savepaths);
}
}
出现的问题如下:
当前正在转换......
D:\JavaProgram\wordtohtml\word\
wordtohtml
com.jacob.com.ComFailException: Invoke Failed: Documents
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent.java)
at wordtohtml.change(wordtohtml.java:62)
at wordtohtml.main(wordtohtml.java:104)
com.jacob.com.ComFailException: Invoke Failed: Quit
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.invoke(ActiveXComponent.java)
at wordtohtml.change(wordtohtml.java:77)
at wordtohtml.main(wordtohtml.java:104)
Exception in thread "main"
大家指教!!
先是路径的问题。本人使用的OS是Windows XP,将jacob.jar添加到classpath没问题,将jacob.dll添加到path却出现问题了,Java程序读不出com.jacob.com.*和com.jacob.activeX.*。最后用一个老办法,把jacob.dll直接复制到system32目录下,总算解决了读“包”的问题。
编译又出现问题了。现将代码和出现的错误写在下面,恳请大家指教:
/*-------------------------------------------------------
-------------------------------------------------------*/
import java.io.File;import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/* //取得指定目录下面所有的doc文件名称*/
public class wordtohtml
{
/* //-------------------------------------------------
//方法原型: change(String paths)
//功能描述: 将指定目录下面所有的doc文件转化为HTML并存储在相同目录下
//输入参数: String
//输出参数: 无
//返 回 值: 无
//其它说明: 递归
//-------------------------------------------- */
public static void change(String paths, String savepaths){
File d = new File(paths);
/* //取得当前文件夹下所有文件和目录的列表*/
File lists[] = d.listFiles();
String pathss = new String("");
/* //对当前目录下面所有文件进行检索*/
for(int i = 0; i < lists.length; i ++)
{
if(lists[i].isFile())
{
String filename = lists[i].getName();
String filetype = new String("");
/* //取得文件类型*/
filetype = filename.substring((filename.length() - 3), filename.length());
/* //判断是否为doc文件*/
if(filetype.equals("doc"))
{
System.out.println("当前正在转换......");
/* //打印当前目录路径*/
System.out.println(paths);
/* //打印doc文件名*/
System.out.println(filename.substring(0, (filename.length() - 4)));
ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
String docpath = paths + filename;
String htmlpath = savepaths + filename.substring(0, (filename.length() - 4));
String inFile = docpath;
/* //要转换的word文件*/
String tpFile = htmlpath+".htm";
/* //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文件*/
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);
flag = true;
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
app.invoke("Quit", new Variant[] {});
}
System.out.println("转化完毕!");
}
}
else
{
pathss = paths;
/* //进入下一级目录*/
pathss = pathss + lists[i].getName() + "\\";
/* //递归遍历所有目录*/
change(pathss, savepaths);
}
}
}
/* //---------------------------------------------------------
//方法原型: main(String[] args)
//功能描述: main文件
//输入参数: 无
//输出参数: 无
//返 回 值: 无
//其它说明: 无
//---------------------------------------------------------- */
public static void main(String[] args)
{
String paths = new String("D:\\JavaProgram\\wordtohtml\\word\\");
String savepaths = new String ("D:\\JavaProgram\\wordtohtml\\html\\");
change(paths, savepaths);
}
}
出现的问题如下:
当前正在转换......
D:\JavaProgram\wordtohtml\word\
wordtohtml
com.jacob.com.ComFailException: Invoke Failed: Documents
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent.java)
at wordtohtml.change(wordtohtml.java:62)
at wordtohtml.main(wordtohtml.java:104)
com.jacob.com.ComFailException: Invoke Failed: Quit
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.invoke(ActiveXComponent.java)
at wordtohtml.change(wordtohtml.java:77)
at wordtohtml.main(wordtohtml.java:104)
Exception in thread "main"
大家指教!!
Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();这个函数调用出错: app.getProperty("Documents").toDispatch() ;
改写成: app.getProperty("document.").toDispatch() ;
你试试,我不能肯定,并且没有测试,不过根据错误显示一定是这个函数设置调用ActiveX的属性名出错。
import com.jacob.activeX.*;
public class jacobTest {
public jacobTest() {
} public static void main(String[] args) {
ActiveXComponent component = new ActiveXComponent("Word.Application");
String inFile = "c:\\aaa.xls";
String tpFile = "c:\\temp.htm";
try {
component.setProperty("Visible", new Variant(false));
Object wordacc = component.getProperty("documents").toDispatch();
Object wordfile = Dispatch.invoke(wordacc, "Open", Dispatch.Method,
new Object[] {inFile, new Variant(false),
new Variant(true)}, new int[1]).toDispatch();
Dispatch.invoke(wordfile, "SaveAs", Dispatch.Method, new Object[] {tpFile,
new Variant(8)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(wordfile, "Close", f);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
component.invoke("Quit", new Variant[] {});
}
}
}
import com.jacob.activeX.*;
public class jacobTest {
public jacobTest() {
} public static void main(String[] args) {
ActiveXComponent component = new ActiveXComponent("Word.Application");
String inFile = "c:\\aaa.doc";
String tpFile = "c:\\temp.htm";
try {
component.setProperty("Visible", new Variant(false));
Object wordacc = component.getProperty("documents").toDispatch();
Object wordfile = Dispatch.invoke(wordacc, "Open", Dispatch.Method,
new Object[] {inFile, new Variant(false),
new Variant(true)}, new int[1]).toDispatch();
Dispatch.invoke(wordfile, "SaveAs", Dispatch.Method, new Object[] {tpFile,
new Variant(8)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(wordfile, "Close", f);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
component.invoke("Quit", new Variant[] {});
}
}
}
MSN:[email protected]
mail:[email protected]
再次谢谢你!
盼着您与我联系!!
编译通过 ,运行出错 :
---------- java ----------
com.jacob.com.ComFailException: Can't get object clsid from progid
at com.jacob.com.Dispatch.createInstance(Native Method)
at com.jacob.com.Dispatch.<init>(Dispatch.java:186)
at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:42)
at jacobTest.main(jacobTest.java:8)
Exception in thread "main"
Output completed (0 sec consumed) - Normal Termination
后来我又到2k server, offices xp 中试过,出现于搂主一样的错误,郁闷中
我想这是因为Microsoft在不同版本的 offices和系统中注册的组件属性名不同的原应吧
去OFFICES网站找找有没有有关说明offices自动化调用属性的文档,如果找到了我会贴出来的
后来用jacobBin_17中的jacob和jacob.dll,结果抛出的异常成了:com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Version
Description: An unknown COM error has occured.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent at DispatchTest.main(DispatchTest.java:13)
Exception in thread "main" com.jacob.com.ComFailException: A COM exception
een 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 DispatchTest.main(DispatchTest.java:35)
是不是com属性,版本的问题,可怎么解决呢?
是jdk版本的问题,用jdk1.3就可以的
to roronoa_zoro():用1.3以后没有搂住得问题了吗?
楼主的代码我试了试,转化成功啦,没什么问题
我是用的jacob1.6,jdk1.3.1_05,win2000professional,word2000
我想问一下:为什么jdk1.4不行呢?