首先我已經做了如下配置:
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 閱讀(4786)
評論(6) 編輯 收藏 所屬分類:
Java