The younger brother of the Development Department asked me how to export the database to word.
I think exporting to word is a very stupid practice, because the word format is not as good as excle or HTML. But I still gave it to him.
A solution.
Since Microsoft has not published the file formats of word and excle, many open-source projects on office in the Java Community have been published.
Jxl, poi, and Jacob should be well-known.
Jxl is mainly used to operate excle documents, while poi and Jacob are relatively powerful.
Jacob uses the JNI method and uses a DLL file written in C ++ to operate office documents. For example, http://danadler.com/jacb/index.html, the latest version 1.7is directly downloaded;
When using Jacob, in addition to including Jacob. jar into classpath, you also need to copy Jacob. DLL to the System32 directory.
There is a helloworld example about writing word:
/*********** Wordbean. Java *****************/
/*
* Creation date: 2005-10-25
*
* To change the template of the generated file in todo, go
* Window-preferences-Java-code style-Code Template
*/
Package poitest;
Import com. Jacob. ActiveX .*;
Import com.jacb.com .*;
Public class wordbean extends java. AWT. Panel
{
Private activexcomponent mswordapp = NULL;
Private dispatch document = NULL;
Public wordbean ()
{
Super ();
}
Public void openword (Boolean makevisible)
{
// Open word if we/'ve not done it already
If (mswordapp = NULL)
{
Mswordapp = new activexcomponent ("word. application ");
}
// Set the visible property as required.
Dispatch. Put (mswordapp, "visible", new variant (makevisible ));
}
Public void createnewdocument ()
{
// Find the documents collection object maintained by word
Dispatch documents = Dispatch. Get (mswordapp, "documents"). todispatch ();
// Call the add method of the documents collection to create
// A new document to edit
Document = Dispatch. Call (events, "add"). todispatch ();
}
Public void inserttext (string texttoinsert)
{
// Get the current selection within word at the moment. If rb8fnb
// A new document has just been created then this will be at pH
// The top of the new doc
Dispatch selection = Dispatch. Get (mswordapp, "selection"). todispatch ();
// Put the specified text at the insertion point
Dispatch. Put (selection, "text", texttoinsert );
}
Public void savefileas (string filename)
{
Dispatch. Call (document, "saveas", filename );
}
Public void printfile ()
{
// Just print the current document to the default printer
Dispatch. Call (document, "printout ");
}
Public void closedocument ()
{
// Close the document without saving changes
// 0 = wddonotsavechanges
//-1 = wdsavechanges
//-2 = wdprompttosavechanges
Dispatch. Call (document, "close", new variant (0 ));
Document = NULL;
}
Public void closeword ()
{
Dispatch. Call (mswordapp, "quit ");
Mswordapp = NULL;
Document = NULL;
}
}
/************ Wordtest. Java **********/
Package jacobtest;
Import java. Io. file;
Import com.jacb.com .*;
Import com. Jacob. ActiveX .*;
Public class wordtest {
Public static void main (string [] ARGs ){
Wordbean word = new wordbean ();
Word. openword (true );
Word. createnewdocument ();
Word. inserttext ("Hello word .");
// Word. savefileas ("E: // testlcl.doc ");
}
}