標籤:word格式轉化為html
package com.wiseweb.util;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant;public class WordtoHtml {//------------------------------------------------------------------------------//方法原型: change(String paths)//功能描述: 將指定目錄下面所有的doc檔案轉化為HTML並儲存在相同目錄下//輸入參數: String//輸出參數: 無//返 回 值: 無//其它說明: 遞迴//------------------------------------------------------------------------------ public static boolean change (String FolderPath,String FileName){ String FileFormat = ""; System.out.println(FolderPath); FileFormat = FileName.substring(FileName.length()-4,FileName.length()); System.out.println(FileFormat); if(FileFormat.equalsIgnoreCase(".doc")) { String DocFile = FolderPath +"//"+ FileName; System.out.println("word檔案路徑:"+DocFile); //word檔案的完整路徑 String HtmlFile = DocFile.substring(0, (DocFile.length() - 4)) + ".html"; System.out.println("htm檔案路徑:"+HtmlFile); //html檔案的完整路徑 ActiveXComponent app = new ActiveXComponent("Word.Application"); //啟動word try { app.setProperty("Visible", new Variant(false)); //設定word程式非可視化運行 Dispatch docs = app.getProperty("Documents").toDispatch(); Dispatch 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]); //作為htm格式儲存檔案 Dispatch.call(doc, "Close",new Variant(false)); //關閉檔案 } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); //退出word程式 } //轉化完畢 return true; } return false; }//------------------------------------------------------------------------------//方法原型: main(String[] args)//功能描述: main檔案//輸入參數: 無//輸出參數: 無//返 回 值: 無//其它說明: 無//------------------------------------------------------------------------------ public static void main(String[] args) { // String paths = new String("E://wordToHtml"); String paths = new String("E:");// String filename = "a.doc"; String filename = "servlet和jsp學習指南(Budi kurniawan).doc"; change(paths, filename); }}其實原理就是利用word把文檔開啟,然後另存新檔html格式就ok了。
Word格式轉化為Html