Posted on 2008-03-28 10:08
oathleo 閱讀(5925)
評論(7) 編輯 收藏 所屬分類:
Java
jacob是java和windows下的com橋,通過它我們可以在java程序中調(diào)用COM組件。如果你的JDK是1.4,那你需要下載jacob1.9的jni庫才能正常運行,早期版本在JDK1.4下有些問題。
package com;
/**
* 〈p〉Title:Word文檔轉(zhuǎn)html類〈/p〉
* 〈p〉Description: 〈/p〉
* 〈p〉Copyright:() 2002〈/p〉
* @author 舵手
* @version 1.0
*/
import com.jacob.com.*;
import com.jacob.activeX.*;
public class WordtoHtml {
/**
*文檔轉(zhuǎn)換函數(shù)
*@param docfile word文檔的絕對路徑加文件名(包含擴展名)
*@param htmlfile 轉(zhuǎn)換后的html文件絕對路徑和文件名(不含擴展名)
*/
public static void change(String docfile, String htmlfile) {
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 啟動word
try {
app.setProperty("Visible", new Variant(false));
//設(shè)置word不可見
Object docs = app.getProperty("Documents").toDispatch();
Object doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
// 打開word文件
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(8) }, new int[1]);
// 作為html格式保存到臨時文件
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[]{});
}
}
public static void main(String[] strs){
WordtoHtml.change("c:\\a\\運輸管理調(diào)度系統(tǒng)總體方案.doc", "c:\\a\\t");
}
}
Feedback
# re: Java Word轉(zhuǎn)html 回復(fù) 更多評論
2008-03-28 10:27 by
支持!
# re: Java Word轉(zhuǎn)html 回復(fù) 更多評論
2008-03-28 16:09 by
good
# re: Java Word轉(zhuǎn)html 回復(fù) 更多評論
2008-03-29 18:50 by
不過這種方式一般都行不通,Java需要跨平臺,綁定Com就綁定Windows了,比較可行的方法就是OpenOffice轉(zhuǎn)換。
# re: Java Word轉(zhuǎn)html 回復(fù) 更多評論
2008-03-30 12:55 by
人家本身就是window平臺下的東西為什么需要用openOffice轉(zhuǎn)換呢,技術(shù)是為應(yīng)用服務(wù)的!
# re: Java Word轉(zhuǎn)html 回復(fù) 更多評論
2008-03-31 12:00 by
我的原創(chuàng)好像也沒這么高的點擊率:)
# re: Java Word轉(zhuǎn)html 回復(fù) 更多評論
2009-07-31 13:10 by
有沒有html轉(zhuǎn)pdf
# re: Java Word轉(zhuǎn)html 回復(fù) 更多評論
2012-05-30 17:35 by
我試了,為什么報錯:The method call(Dispatch, String, Object) in the type Dispatch is not applicable for the arguments (Object,
String, Variant),包名可以是test嗎?自己設(shè)的@舵手