Java platform, use openoffice to convert word to html

Source: Internet
Author: User

From: http://hi.baidu.com/trstones/blog/item/7fca145426436c57d0090663.html

There is no simple way to convert word to html in java. The openOffice implementation should be regarded as "Pull the big ones in the cookbook ".

1. First download openOffice. This is a third-party open-source project dedicated to writing similar word documents in the java environment (if a word cannot be edited, java will become a role in the layman's mind ). I downloaded OpenOffice.org 3.2.

2. Download and install. Run cmd to go to the "installation directory \ OpenOffice.org 3 \ program" folder. Run the following command:

Soffice-headless-accept = "socket, host = 127.0.0.1, port = 8100; urp;"-nofirststartwizard

It means to start a service of openoffice for use by other programs (look at how generous our open-source leaders are, not as stingy as Microsoft, for fear of using them ).

3. Test whether port 8100 can be used. Run the cmd command "telnet localhost 8100". If it is enabled, there will be a screen that cannot be blacklisted. If it is not enabled, a connection failure message will appear.

4. Download The jodconverter project, which is 2.2.2. (We will not duplicate the wheel. Just get on the bus !)

5. Create your own project and reference all jar packages in the lib folder of jodconverter. Then write the following code

Public static void main (String args []) {
File inputFile = new File ("D: \ test \ Ad test .doc ");
File outputFile = new File ("D: \ test \ Ad test .html ");
OpenOfficeConnection connection = new SocketOpenOfficeConnection (8100 );
Try {
Connection. connect ();
} Catch (Exception e ){
E. printStackTrace ();
}
DocumentConverter converter = new OpenOfficeDocumentConverter (connection );
Converter. convert (inputFile, outputFile );
Connection. disconnect ();
}

Then run it. It should be okay. Of course, "D: \ test \ Ad test .doc" must exist in advance (the program cannot be sold independently ).

6. Others. The above Code doc-> html;
For xls-> html, change "D: \ test \ Ad test .doc" to "D: \ test \ ***. xls ";
If it is doc-> example, you can change the advertisement test .html#into an advertisement test example ".
Let's test the two. Let's explore them by yourself. You can open the openoffice client program and check the files that are supported. You can save the files in those formats. Good Fool!

 

Related Article

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.