今天瀏覽CSDN論壇,無意中看到了一篇關於JACOB的帖子
名爲:用java 將doc轉換成html文件...
http://community.csdn.net/Expert/TopicView1.asp?id=5497442
接着就看了一下有關JACOB的網站,該項目現在已經在sourceforge.net上發佈.
http://sourceforge.net/projects/jacob-project/
JACOB is a JAVA-COM Bridge that allows you to call COM Automation components from Java. It uses JNI to make native calls into the COM and Win32 libraries.
雖然目前項目中還用不到,但是知道一下也是好的.以後用到的時候不用再費盡心思滿Google找了,呵呵.
標記一下.
下面還有一部分JACOB應用的實例和注意,來自http://shuxudong.blog.hexun.com/7591618_d.html
上面是些如下內容的目的,希望你也清楚。
/**
* @author sxd1
* @param paths
* @param savepaths
* @see 完成從word文檔向HTML的轉換
*/
public static void change(String paths, String savepaths) {
File[] lists = getFileList(paths);
String pathss = new String("");
// 對當前目錄下面所有文件進行檢索
for (int i = 0; i < lists.length; i++) {
if (lists[i].isFile()) {
String filename = lists[i].getName();
// 判斷是否爲doc文件
if (filename.endsWith("doc")) {
ActiveXComponent app = new ActiveXComponent(
"Word.Application");// Excel.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不可見
Object docs = app.getProperty("Documents").toDispatch();
Object doc = Dispatch.invoke((Dispatch) docs, "Open",
Dispatch.Method, new Object[] { inFile },
new int[1]).toDispatch();
// 打開word文件
Dispatch.invoke((Dispatch) doc, "SaveAs",
Dispatch.Method, new Object[] { tpFile,
new Variant(8) }, new int[1]);
// 作爲html格式保存到臨時文件
Variant f = new Variant(false);
Dispatch.invoke((Dispatch) doc, "Close",
Dispatch.Method, new Object[] { new Variant(
false) }, new int[1]);
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);
}
}
}
上面的代碼是程序的主體。
/**
* @author sxd1
* @param dirName
* @return string[]
* @see 得到給定目錄下面的所有文件名稱,並存放到string[]中
*/
private static File[] getFileList(String dirName) {
File[] fileList = null;
File dir = new File(dirName);
if (dir.isDirectory())
fileList = dir.listFiles();
return fileList;
}
這裏需要注意的是:程序是用jadoc包完成的。在寫程序之前需要完成下面的工作。因爲JDK裏面沒有這個包。
1,下載jadoc.jar包,可以訪問:http://danadler.com/jacob/,找Version1.7
2,需要注意的是Version1.7中包括jadoc.jar和jadoc.dll兩個文件但是在這裏只能用jadoc.jar,jardoc.dll需要重新下載。下載地址:http://tech.groups.yahoo.com/group/jacob-project/點“files”(當然你必須事先註冊並登陸並加入組,才能進入)找就jacod_jer142fix_bin.zip並下載。
3,將下載的jacod.jar拷貝到jdk的/jre/lib/ext下面如:C:/jdk1.5.0_01/jre/lib/ext
4,將下載的jacod.dll拷貝到jdk的jre/bin下面如:C:/jdk1.5.0_01/jre/bin
然後就可以運行了,祝成功!