package com; /** * 〈p〉Title:Word文檔轉Html類〈/p〉 * 〈p〉Description: 〈/p〉 * 〈p〉Copyright:() 2002〈/p〉 * @author 舵手 * @version 1.0 */ import com.jacob.com.*; import com.jacob.activeX.*; public class WordtoHtml { /** *文檔轉換函數 *@param docfile word文檔的絕對路徑加文件名(包含擴展名) *@param htmlfile 轉換後的html文件絕對路徑和文件名(不含擴展名) */ public static void change(String docfile, String htmlfile) { ActiveXComponent app = new ActiveXComponent("Word.Application"); // 啟動word try { app.setProperty("Visible", new Variant(false)); //設置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運輸治理調度系統總體方案.doc", "c:a "); } }