首先我已经做了如下配置:
1.导入 jacob.jar 这个包。
2.把 jacob.dll 拷贝到 C:\WINDOWS\system32 目录下。
以上的两个文件您可以到网上http://danadler.com/jacob/ 下载的1.7的版本
jacob.jar 与 jacob.dll 版本一致。
代码如下:
package one;
import com.jacob.com.*;
import com.jacob.activeX.*;
import java.io.*;
//取得指定目录下面所有的doc文件名称
public class wordtohtml2 {
// ------------------------------------------------------------------------------
// 方法原型: change(String paths)
// 功能描述: 将指定目录下面所有的doc文件转化为HTML(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;
// HTML文件
boolean flag = false;
try {
app.setProperty("Visible", new Variant(false));
// 设置word不可见
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch 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);
}
}
}
public static void main(String[] args) {
String paths = new String("e:\\english\\ebook\\");
String savepaths = new String("e:\\english\\ebook\\");
change(paths, savepaths);
}
}
运行后出现的错误为:
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 one.WordtoHtml.change(WordtoHtml.java:25)
at one.WordtoHtml.main(WordtoHtml.java:39)
Exception in thread "main" 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 one.WordtoHtml.change(WordtoHtml.java:35)
at one.WordtoHtml.main(WordtoHtml.java:39)
附加:下面是总结了的错误!
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下
posted on 2008-03-29 02:16
EricWong 阅读(4781)
评论(6) 编辑 收藏 所属分类:
Java