Java converts word to HTML or TXT

Source: Internet
Author: User

Recently, you need to preview the Word file on the page. Although word itself can be opened on the page, there are two drawbacks: 1. You must install word on the client, 2. There are differences between the client environment and the Office version, which may cause unstable preview. I found it on the Internet and found that poi could replace word with txt, but all the formats were lost. Only the bald text was found, and Jacob was searched, finally, download Jacob from SourceForge and read the doc.

1 goto http://sourceforge.net/projects/jacob-project/ and Download latest library of Jacob.

The structure of the downloaded ZIP file is as follows:

2 Intel CPU machine copy jacob-1.15-M3-x86.dll to % java_home %/JRE/bin, amd cpu machine copy jacob-1.15-M3-x64.dll. however, make sure that the JRE directory is the JRE you are using, because many eclipse versions now include JRE. this can be viewed in eclipse Windows> preferences> installed jres.

3. Copy Jacob. jar to the Lib of your project directory and ensure that the classpath is added.

After the preparation is complete, write the program now.

View plaincopy to clipboardprint?
Import com. Jacob. ActiveX. activexcomponent;
Import com.jacb.com. Dispatch;
Import com.jacb.com. Variant;
Public class test {
Public static void main (string [] ARGs ){
Activexcomponent APP = new activexcomponent ("word. application ");
App. setproperty ("visible", new variant (false ));
Dispatch doc1 = app. getproperty ("events"). todispatch ();
// Open aaaa.doc
Dispatch doc2 = Dispatch. Invoke (
Doc1,
"Open ",
Dispatch. method,
New object [] {"E: // aaaa.doc", new variant (false), new variant (true )},
New int [1]
). Todispatch ();
// The URL is aaaa.html.
Dispatch. Invoke (
Doc2,
"Saveas ",
Dispatch. method,
New object [] {
"C: // aaaa.html ",
New variant (8) // 7 is in TXT format, and 8 is saved in HTML Format
},
New int [1]
);
Variant F = new variant (false );
Dispatch. Call (doc2, "close", F );
}
}
Import com. Jacob. ActiveX. activexcomponent;
Import com.jacb.com. Dispatch;
Import com.jacb.com. Variant;
Public class test {
Public static void main (string [] ARGs ){
Activexcomponent APP = new activexcomponent ("word. application ");
App. setproperty ("visible", new variant (false ));
Dispatch doc1 = app. getproperty ("events"). todispatch ();
// Open aaaa.doc
Dispatch doc2 = Dispatch. Invoke (
Doc1,
"Open ",
Dispatch. method,
New object [] {"E: // aaaa.doc", new variant (false), new variant (true )},
New int [1]
). Todispatch ();
// The URL is aaaa.html.
Dispatch. Invoke (
Doc2,
"Saveas ",
Dispatch. method,
New object [] {
"C: // aaaa.html ",
New variant (8) // 7 is in TXT format, and 8 is saved in HTML Format
},
New int [1]
);
Variant F = new variant (false );
Dispatch. Call (doc2, "close", F );
}
}

It is easy to use.

Of course, Jacob can not only do word to HTML, but also do a lot of things:

Jacob is a Java library that lets Java applications communicate with Microsoft Windows DLLs or com libraries. it does this through the use of a custom DLL that the Jacob Java classes communicate with via JNI. the Library and DLL isolate the Java developer from the underlying windows libraries so that the Java developer does not have to write custom JNI code.

More functions are available only when necessary.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/sunxing007/archive/2010/05/19/5609404.aspx

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.