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