Java invokes word with Jacob (generates dynamic content from Word templates)

Source: Internet
Author: User
Tags save file

This blog is a guide to Jacob's article
Java operations the Microsoft Word Jacob
Introduction to Jacob's use and problem resolution
Use Jacob to convert Word to HTML or txt
Use Jacob to convert Word to PDF
Java invokes word with Jacob (generates dynamic content from Word templates)

The principle of jcob is to manipulate the ActiveX object of Word through an interface. The current version is 1.9.
First: Copy the Jcob.dll to the System32 directory.
Second: Place the Jcob.jar path in the Classpath directory.
Finally: Write the Access class.

public static void Main (string[] args) {//starts Word, generates a Activexcomponent object activexcomponent app = new Activexcomponent ("Wor D.application "); The Word file to convert String inFile = "D://project//java_word//java_word//wordtemplet.doc"; The destination file to be saved String tpfile = "D://project//java_word//java_word//wordtemplet3.doc"; Boolean flag = false; Dispatch XLO = App.getobject (); String oldtext= "1234"; String newtext= "Test"; Boolean visible=false; String bookmarkkey= "Lb_kjgg"; try {//Set Word not visible app.setproperty ("Visible", New Variant (Visible)),//log.info ("Set Word not visible success!"); System.out.println ("Set Word not visible success!"); Dispatch docs = app.getproperty ("Documents"). Todispatch (); Dispatch doc = Dispatch.invoke (docs, "Open", Dispatch.method, New Object[]{infile, new variant (FALSE), new variant (TRUE)} , new Int[1]). Todispatch (); Open the Word file//Application username attribute in word2003 VBA document. String username=app.getpropertyasstring ("UserName"); System.out.println ("User name:" +username); Dispatch Selection=app.getproperty ("Selection"). Todispatch ();Get a component System.out.println ("Selection"); Dispatch find = App.call (selection, "find"). Todispatch (); Look for what text Dispatch.put (Find, "text", OldText); Replace text Dispatch.call (Find, "clearformatting"); Dispatch.put (Find, "Text", OldText); Dispatch.call (Find, "Execute"); Dispatch.put (Selection, "Text", NewText); Dispatch.call (App, "SaveAs", inFile); SYSTEM.OUT.PRINTLN ("Replace"); Sets the specified value to the specified label to Dispatch activedocument=app.getproperty ("ActiveDocument"). Todispatch (); System.out.println ("ActiveDocument"); Dispatch BookMarks = App.call (ActiveDocument, "BookMarks"). Todispatch (); System.out.println ("bookmarks"); Boolean Bookmarkexist1=dispatch.call (BookMarks, "Exists", Bookmarkkey). ToBoolean (); if (bookmarkexist1==true) {System.out.println ("exists bookmark!"); Dispatch Rangeitem = Dispatch.call (BookMarks, "Item", Bookmarkkey). Todispatch (); System.out.println ("Range item!"); Dispatch range = Dispatch.call (Rangeitem, Range). Todispatch (); SYSTEM.OUT.PRINTLN ("Range!"); Tag Value String Bookmarkvalue=disPatch.get (range, "Text"). ToString (); bookmarkvalue= "Test"; if (bookmarkvalue!=null) {dispatch.put (range, Text, new Variant (Bookmarkvalue));} else{System.out.println ("Not exists bookmark!");}//Save file Dispatch.invoke (Doc, "SaveAs", Dispatch.method, new object[] { Tpfile, New Variant (0)}, new int[1]); Save to the destination file as Word format variant F = new Variant (FALSE); Dispatch.call (Doc, "Close", f); Flag = true; catch (Exception e) {e.printstacktrace ();} finally {App.invoke ("Quit", new variant[] {});}

Blog Source: http://blog.csdn.net/j2eejia/archive/2005/03/11/317152.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.