Some time ago, I had a headache for this problem. Later I checked the materials and finally solved the problem. Now I am releasing the program for future reference. //------------------------------------------------------------------------------ // Copyright (c) inspur group Commercial Systems Ltd. reserves all rights // File name: wordtohtml file version: 1.00.00 // Author: Guo casting author Email: guozhu@langchao.com completion date: // File description: // Other descriptions: // Class column table: // Wordtohtml: Convert all DOC files in the specified directory to HTML and store them in the same directory. // Modification history: // # Version modification date modified by the author //---------------------------------------------------------------------------- // 1 1.00.01 description of the author's name Modification //---------------------------------------------------------------------------- //------------------------------------------------------------------------------ Import com.jacb.com .*; Import com. Jacob. ActiveX .*; Import java. Io .*; // Obtain the names of all DOC files under the specified directory Public class wordtohtml { //------------------------------------------------------------------------------ // Method prototype: Change (string paths) // Function Description: Convert all DOC files in the specified directory to HTML and store them in the same directory. // Input parameter: String // Output parameter: None // Return value: None // Other instructions: Recursion //------------------------------------------------------------------------------ Public static void change (string paths, string savepaths) {
File d = new file (paths ); // Obtain the list of all files and directories in the current folder File lists [] = D. listfiles (); String pathss = new string (""); // Search all files in the current directory For (INT I = 0; I <lists. length; I ++) { If (lists [I]. isfile ()) { String filename = lists [I]. getname (); String filetype = new string (""); // Obtain the file type Filetype = filename. substring (filename. Length ()-3), filename. Length ());
// Determine whether the file is a DOC file If (filetype. Equals ("Doc ")) { System. Out. println ("converting ......"); // Print the current directory path System. Out. println (paths ); // Print the DOC file name System. Out. println (filename. substring (0, (filename. Length ()-4 )));
Activexcomponent APP = new activexcomponent ("word. application"); // start the word
String docpath = paths + filename; String htmlpath = savepaths + filename. substring (0, (filename. Length ()-4 ));
String infile = docpath; // The Word file to be converted String tpfile = htmlpath; // Html file Boolean flag = false;
Try { App. setproperty ("visible", new variant (false )); // Set word invisible Object docs = app. getproperty ("events"). todispatch (); Object Doc = dispatch. invoke (Docs, "open", dispatch. method, new object [] {infile, new variant (false), new variant (true)}, new int [1]). todispatch (); // Open the Word file Dispatch. Invoke (Doc, "saveas", dispatch. method, new object [] {tpfile, new variant (8)}, new int [1]); // Save it as a temporary file in HTML Format Variant F = new variant (false ); Dispatch. Call (Doc, "close", F ); Flag = true; } Catch (exception E) { E. printstacktrace (); } Finally { App. Invoke ("quit", new variant [] {}); } System. Out. println ("converted! "); } } Else { Pathss = paths; // Enter the directory at the next level Pathss = pathss + lists [I]. getname () + ""; // Recursively traverse all directories Change (pathss, savepaths ); } }
} //------------------------------------------------------------------------------ // Method prototype: Main (string [] ARGs) // Function Description: main file // Input parameter: None // Output parameter: None // Return value: None // Other instructions: None //------------------------------------------------------------------------------ Public static void main (string [] ARGs) {
String paths = new string ("D: work2004.10.8 est system est01word "); String savepaths = new string ("D: work2004.10.8 est system est01html "); Change (paths, savepaths ); } } The jar package for import is an open source, which can be searched on the Internet. Dispatch. Invoke (Doc, "saveas", dispatch. method, new object [] {tpfile, new variant (8)}, new int [1]); Modify variant (8)} to convert Word to various types.
|