Document introduction to this blog jacob
Java operations on Microsoft Word jacob
Getting started with jacob and troubleshooting
Convert Word to Html or txt using Jacob
Convert Word to PDF using Jacob
Java calls word through jacob (dynamic content is generated based on the Word template)
// Copyright <br/> // copyright (c) inspur group Commercial Systems Ltd. reserves all rights <br/> // file name: wordtohtml file version: 1.00.00 <br/> // prepared by: Guo casting author Email: guozhu@langchao.com completion date: 2004-10-20 <br/> // file Description: <br/> // other description: <br/> // class column table: <br/> // wordtohtml: convert all DOC files in the specified directory to HTML and store them in the same directory <br/> // modify the history: <br/> // # modified by the author of the version modification date <br/> //--------------------------------------- ----------------------------------- <Br/> // 1 1.00.01 2004-10-14 author Name Description <br/> // description <br/> Import com.jacb.com. *; <br/> Import COM. jacob. activeX. *; <br/> Import Java. io. *; <br/> // get the names of all DOC files under the specified directory <br/> public class wordtohtml <br />{< Br/> // ------------------------------------------------------------------------------ <br/> // method prototype: Change (string paths) <br/> // function description: convert all DOC files in the specified directory to HTML and store them in the same directory <br/> // input parameter: String <br/> // output parameter: none <br/> // return value: None <br/> // other instructions: recursion <br/> // -------------------------------------------------------------------------------- <br/> Public static void change (string paths, string savepaths) <br/ >{< Br/> <br/> file D = new file (paths ); <br/> // obtain the list of all files and directories in the current folder <br/> file lists [] = D. listfiles (); <br/> string pathss = new string (""); </P> <p> // search all files in the current directory <br/> for (INT I = 0; I <lists. length; I ++) <br/>{< br/> If (lists [I]. isfile () <br/>{< br/> string filename = lists [I]. getname (); <br/> string filetype = new string (""); <br/> // obtain the file type <br/> filetype = filename. substring (filena Me. length ()-3), filename. length (); <br/> // determine whether the file is a DOC file <br/> If (filetype. equals ("Doc") <br/>{< br/> system. out. println ("converting ...... "); <br/> // print the current directory path <br/> system. out. println (paths); <br/> // print the DOC file name <br/> system. out. println (filename. substring (0, (filename. length ()-4); <br/> activexcomponent APP = new activexcomponent ("word. application "); // start WORD <br/> string docpat H = paths + filename; <br/> string htmlpath = savepaths + filename. substring (0, (filename. length ()-4); <br/> string infile = docpath; <br/> // The Word file to be converted <br/> string tpfile = htmlpath; <br/> // html file </P> <p> Boolean flag = false; <br/> try <br/> {<br/> app. setproperty ("visible", new variant (false); <br/> // set word invisible </P> <p> Object docs = app. getproperty ("events "). todispatch (); <Br/> Object Doc = dispatch. invoke (Docs, "open", dispatch. method, new object [] {infile, new variant (false), new variant (true)}, new int [1]). todispatch (); <br/> // open the Word file <br/> dispatch. invoke (Doc, "saveas", dispatch. method, new object [] {tpfile, new variant (8)}, new int [1]); <br/> // save it as a temporary file in HTML format <br/> variant F = new variant (false); <br/> dispatch. call (Doc, "close", f); <br/> flag = true; <br/>}< br /> Catch (exception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/> finally <br/>{< br/> app. invoke ("quit", new variant [] {}); <br/>}< br/> system. out. println ("converted! "); <Br/>}< br/> else <br/>{< br/> pathss = paths; <br/> // enter the directory of the next level <br/> pathss = pathss + lists [I]. getname () + "//"; <br/> // recursively traverse all directories <br/> change (pathss, savepaths ); <br/>}< br/> // method prototype <br/> // method: main (string [] ARGs) <br/> // Function Description: main file <br/> // input parameter: None <br/> // output parameter: none <br/> // return value: None <br/> // other instructions: none <br/> // -------------------------------------------------------------------------------- <br/> Public static void main (string [] ARGs) <br/>{< br/> <br/> string paths = new string ("D: // work // 2004.10.8 // test system // test01 // word // "); <br/> string savepaths = new string (" D: // work // 2004.10.8 // test system // test01 // html // "); </P> <p> change (paths, savepaths ); </P> <p >}< br/>}</P> <p>
Blog Source: http://blog.csdn.net/hcx_2008/archive/2007/09/11/1781375.aspx