Package Com.wiseweb.util;import Com.jacob.activex.activexcomponent;import Com.jacob.com.dispatch;import Com.jacob.com.variant;public class Wordtohtml {//---------------------------------------------------------------- --------------//Method Prototype: Change (String paths)//Function Description: Converts all doc files under the specified directory to HTML and stores them in the same directory//input parameters: string//output parameter: none//return value: none// Other instructions: recursive//------------------------------------------------------------------------------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 File path:" +docfile); The full path of the Word file String htmlfile = docfile.substring (0, (Docfile.length ()-4)) + ". html"; System.out.println ("HTM file path:" +htmlfile); The full path of the HTML file activexcomponent app = new Activexcomponent ("Word.Application"); Start Word try {app.setproperty ("Visible", new Variant (false)); Set Word program non-visual to run 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 (); Open Word file Dispatch.invoke (doc, "SaveAs", Dispatch.method, New Object[]{htmlfile,new Variant (8)}, new int[1]) ; Save the file as an HTM format dispatch.call (doc, "Close", new Variant (false)); Close file} catch (Exception e) {e.printstacktrace (); } finally {App.invoke ("Quit", new variant[] {}); Quit Word Program}//Conversion complete RETurn true; } return false; }//------------------------------------------------------------------------------//Method prototype: Main (string[] args)// Function Description: Main file//input parameter: none//output parameter: none//return value: none//Other Description: no//------------------------------------------------------------------ ------------public static void Main (string[] args) {//String paths = new String ("e://wordtohtml"); String paths = new String ("E:");//String filename = "A.doc"; String filename = "servlet and JSP Learning Guide (Budi Kurniawan). doc"; Change (paths, filename); }}
In fact, the principle is to use Word to open the document, and then save as HTML format is OK.
Word format converted to HTML