A small example on chinajavaworld inserts a sentence into the word and sets the paragraph style.
Address: http://bbs.chinajavaworld.com/thread.jspa? Threadid = 725047
Jacob is a bridge from Java to Microsoft's COM interface. Use Jacob to allow any JVM to access COM objects so that Java applications can call COM objects. If you want to process the MS Word, Excel, Jacob is a good choice. Jacob has now become an open source project of SourceForge (http://sourceforge.net/projects/jacob-Project/), the version used in this article is 1.10.1.
This technology was used in the project, but I did not find an article on the Internet that fits the topic. After my own exploration, I wrote this article.
This article may not fully meet your requirements. You can also follow my exploration methods: refer to the books on using Office Components in VBA, and then refer to the following tip to complete the required functions. The complete test code is provided at the end of the article.
Brief Introduction to generating Word documents:
1. it is very important to initialize the com thread. Otherwise, the can't co-create object exception will occur during the Second Creation of the COM object (see Jacob's help documentation ), call the reallocate method after the COM component is operated.
Comthread. initsta (); // It is very important to initialize the com thread !! Call the reallocate method after use.
2. initialize the word application, create a blank document, and obtain the document content object.// Instantiate objword // declare word object
Activexcomponent objword = new activexcomponent ("word. application ");
// Assign a local word object
Dispatch wordobject = (dispatch) objword. GetObject ();
// Create a dispatch parameter to show the document that is opened
Dispatch. Put (dispatch) wordobject, "visible", new variant (true); // new variant (true) indicates that the word application is visible
Tip: when setting the attributes of an object, assign values to the attributes using the PUT Method of dispatch. The preceding statement is equivalent to the wordobject. Visible = true statement of VB.
// Instantiate the specified ents Property
Dispatch documents ents = objword. getproperty ("documents"). todispatch (); // documents indicates all document windows of Word (word is a multi-document application)
// Add a new word document, current active document
Dispatch document = Dispatch. Call (documents, "add"). todispatch (); // use the Add command to create a new document. Use the open command to open an existing document.
Tip: when calling an object method, use the call method of dispatch. The preceding statement is equivalent to the document = documents. Add () Statement of VB.
Dispatch wordcontent = Dispatch. Get (document, "content"). todispatch (); // get the content of the Word file
Tip: When obtaining a member variable (attribute) of an object, use the get method of dispatch. The preceding statement is equivalent to the wordcontent = Document. Content Statement of VB.
3. After obtaining the content of a Word document, you can perform operations on the content.
Dispatch. Call (wordcontent, "insertafter", "content of a paragraph"); // insert a paragraph
4. Set the text format of the inserted paragraph
Dispatch paragraphs = Dispatch. Get (wordcontent, "paragraphs"). todispatch (); // all paragraphs
Int paragraphcount = Dispatch. Get (paragraphs, "Count"). toint (); // The total number of paragraphs
// Locate the entered paragraph and set the format
Dispatch lastparagraph = Dispatch. Call (paragraphs, "item ",
New variant (paragraphcount )).
Todispatch (); // the last segment
Dispatch lastparagraphrange = Dispatch. Get (lastparagraph, "range ").
Todispatch ();
Dispatch font = Dispatch. Get (lastparagraphrange, "font"). todispatch ();
Dispatch. Put (font, "bold", new variant (true); // you can specify this parameter as a blacklist.
Dispatch. Put (font, "italic", new variant (true); // set it to italic.
Dispatch. Put (font, "name", new variant (" ant "));//
Dispatch. Put (font, "size", new variant (12); // small four
Note: If you want to insert a new blank line, you also need to set the text format of the paragraph. Otherwise, the text format of the newly inserted line will be the same as that of the newly inserted paragraph.
5. Save the current document
Dispatch. Call (document, "saveas", new variant ("C:
Abc.doc "); // save a new document
6. Release the com thread
Comthread. Release (); // release the com thread. According to Jacob's help document, com thread collection cannot be processed by Java's Garbage Collector
Code Section:
Import com. Jacob. ActiveX. activexcomponent;
Import com.jacb.com. Dispatch;
Import com.jacb.com. Variant;
Import com.jacb.com. comthread;
Public class studyjacob ...{
Public static void main (string [] ARGs )...{
Comthread. initsta (); // It is very important to initialize the com thread !! Call the reallocate method after use.
// Instantiate objword // declare word object
Activexcomponent objword = new activexcomponent ("word. application ");
// Assign a local word object
Dispatch wordobject = (dispatch) objword. GetObject ();
// Create a dispatch parameter to show the document that is opened
Dispatch. Put (dispatch) wordobject, "visible", new variant (true); // new variant (true) indicates that the word application is visible
// Instantiate the specified ents Property
Dispatch documents ents = objword. getproperty ("documents"). todispatch (); // documents indicates all document windows of Word (word is a multi-document application)
// Add a new word document, current active document
Dispatch document = Dispatch. Call (documents, "add"). todispatch (); // use the Add command to create a new document. Use the open command to open an existing document.
Dispatch wordcontent = Dispatch. Get (document, "content"). todispatch (); // get the content of the Word file
Dispatch. Call (wordcontent, "insertafter", "content of a paragraph"); // insert a paragraph
Dispatch paragraphs = Dispatch. Get (wordcontent, "paragraphs"). todispatch (); // all paragraphs
Int paragraphcount = Dispatch. Get (paragraphs, "Count"). toint (); // The total number of paragraphs
// Locate the entered paragraph and set the format
Dispatch lastparagraph = Dispatch. Call (paragraphs, "item ",
New variant (paragraphcount )).
Todispatch (); // the last segment
Dispatch lastparagraphrange = Dispatch. Get (lastparagraph, "range ").
Todispatch ();
Dispatch font = Dispatch. Get (lastparagraphrange, "font"). todispatch ();
Dispatch. Put (font, "bold", new variant (true); // you can specify this parameter as a blacklist.
Dispatch. Put (font, "italic", new variant (true); // set it to italic.
Dispatch. Put (font, "name", new variant (" ant "));//
Dispatch. Put (font, "size", new variant (12); // small four
Dispatch. Call (document, "saveas", new variant ("C:
Abc.doc "); // save a new document
Comthread. Release (); // release the com thread. According to Jacob's help document, com thread collection cannot be processed by Java's Garbage Collector
}
}
The comments of each statement are also very detailed. You can try to do it. The program runs properly. As a simple example.