Use Java to convert Word to HTML or TXT!

Source: Internet
Author: User

Guidance:
Use Java to convert Word to HTML or TXT. 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 groovy 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 Path = new string ("");
// Search all files in the current directory
For (INT I = 0; I {
If (lists. isfile ())
{
String filename = lists. getname ();
String filetype = new string ("");
// Obtain the file type
Filetype = filename. Su tring (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. Su tring (0, (filename. Length ()-4 )));
  
Activexcomponent A = new activexcomponent ("word. A lication"); // start the word
  
String docpath = paths filename;
String htmlpath = savepaths filename. Su tring (0, (filename. Length ()-4 ));
  
String infile = docpath;
// The Word file to be converted
String tpfile = htmlpath;
// Html file
Boolean flag = false;
  
Try
{
A. setproperty ("visible", new variant (false ));
// Set word invisible
Object docs = A. getproperty ("events"). todi atch ();
Object Doc = di atch. invoke (Docs, "open", di atch. method, new object [] {infile, new variant (false), new variant (true)}, new int [1]). todi atch ();
// Open the Word file
Di atch. Invoke (Doc, "saveas", di atch. method, new object [] {tpfile, new variant (8)}, new int [1]);
// Save it as a temporary file in HTML Format
Variant F = new variant (false );
Di atch. Call (Doc, "close", F );
Flag = true;
}
Catch (exception E)
{
E. printstacktrace ();
}
Finally
{
A. Invoke ("quit", new variant [] {});
}
System. Out. println ("converted! ");
}
}
Else
{
Path = path
// Enter the directory at the next level
Path = path lists. getname ()"//"
// Recursively traverse all directories
Change (path, 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: // work // 2004.10.8 // test system // test01 // word //");
String savepaths = new string ("D: // work // 2004.10.8 // test system // test01 // html //");
Change (paths, savepaths );
}
}
The jar package for import is an open source, which can be searched on the Internet.
Di atch. Invoke (Doc, "saveas", di atch. method, new object [] {tpfile, new variant (8)}, new int [1]);
Modify variant (8)} to convert Word to various types.

This article is transferred from
Http://www.phpap.com/view.php? Tid = 1630

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.