Use Jcob to convert Word to Html or txt

Source: Internet
Author: User
//------------------------------------------------------------------------------
// 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: // Work // 2004.10.8 // test system // test01 // word //");
String savepaths = new String ("D: // Work // 2004.10.8 // test system // test01 // html //");

Change (paths, savepaths );

}
}

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.