Val's Blog
Lots of stuff for Web 2.0 freaks and Java addicts
Feeds RSS | Atom | RDF
 
 
Albert Einstein: "Intellectuals solve problems: geniuses prevent them."
[ Login ]

October 2004
SunMonTueWedThuFriSat
      1  2 
 3  4  5  6  7  8  9 
 10  11  12  13  14  15  16 
 17  18  19  20  21  22  23 
 24  25  26  27  28  29  30 
 31       
Sep  |  Today  |  Nov
XML Feeds   Subscribe with Bloglines

Javaranch Sheriff   My LinkedIn Profile
Drop me a line or two   Bloglines Blogroll
JavaRSS   Referers
How cool are you?   My Reviews

Next trips...
SpringOne 2008 (Jun 11-12, 08)
Ajax Exp. 2008 (Sep 29-Oct 1, 08)
Top 10 entries (#hits)
(As of Nov 30, 2007)


Top 10 entries (#hits/day)
Come Back (5.032)
(As of Nov 30, 2007)
Recent Blog Entries
Recent Blog Comments
Re: Review of "Marketing Management 12th"
i know marketing management by kotler is good book but the problem is that the management part of this book is totally missing as fare as i know managemet is complete different subject and it should not be mixed i am student of MBA i was looking at ass...

Re: Review of "Pro Spring"
Using simple POJOs + factories without Spring for "echo" and "counter" would be a lot more easier. No need to write those XML files... So, in this case using Spring makes me write a lot more code... (OK, you can generate everything with the help of And...

pls urgent
Hi I am trying to generate the word doc but i m not understanding wats happening any one pls figure it out /* * WordAPI.java * * Created on May 30, 2006, 10:50 AM * * To change this template, choose Tools | Template Manager * and open the te...
Archives (# entries)
Links
Other Blogs
Other Blogs

Reviewing
Reading
Locations of visitors to this page
What they once said...
 

Ever needed to generate Word documents from your Java applications? There are only a few tools that allow you to do that. The Apache POI team has developed an API for manipulating Excel files (org.apache.poi.hssf packages) and is currently developing one for Word files also (org.apache.poi.hwpf packages). However, the latter is still in early beta stages and is not bundled with the 2.0 release, so you have to go into CVS and get the files by yourself.

If this suffices, then you are good to go. Otherwise I can propose another solution. A German software company, called Müller & Stein Software, proposes on its website a free component (yes, as in "free" beer) that you can call directly from your Java application to generate any kind of Word files. The downside is that it won't let you read the Word file, though. You're gonna have to use the POI HWPF API if you need that functionality.

The way the whole thing works is incredibly easy. In the bundle, you will find three key files:

  • WordAPI.exe: Word driver component (2000/97) for any Windows platform (XP/2000/NT/ME/98/95)
  • WordProcessing.java: Main class to use from your Java applications to interact with the Word driver
  • SampleTemplate.dot: A template Word file with which you will customize the generation

All you have to do is to define bookmarks (Insert/Bookmark) in the template Word file and give them a name. This name will let you identify what goes where when you generate the Word file. Then, from you Java application, you can write code like:


1. WordProcessing.createNewDocumentFromTemplate("SampleTemplate");
2. WordProcessing.typeTextAtBookmark("AddressLine1", "O'Reilly & Associated, Inc.");
3. WordProcessing.typeTextAtBookmark("AddressLine2", "Mr Miller");
4. WordProcessing.typeTextAtBookmark("AddressLine3", "101 Moris Street");
5. WordProcessing.typeTextAtBookmark("AddressLine4", "Sebastopol, CA 95472-9902");
6. WordProcessing.typeTextAtBookmark("Salutation", "Dear Mr Miller,");
7. WordProcessing.exec();

Let me explain a little further what this code does:

  • On line 1, we let the driver create a new Word document by giving it a hardcoded template file name (without the extension!). There is another alternative in which you can let the user dynamically choose the template to use. Just use the method createNewDocumentFromTemplateToSelectByUser() instead
  • Lines 2 through 6 use the typeTextAtbookmark() method to send strings (2nd argument) to the given bookmark (1st argument). It is also possible to directly send an array of String objects in one shot instead of having to repeatedly call the typeTextAtbookmark() method.
  • Finally, you just invoke the exec() method to generate the Word file. Actually, until you call exec(), all the WordProcessing call does is to write commands into a temporary file that the Word driver will use.

Before calling exec(), you have the choice to call a variety of other methods as described below:

  • saveDocumentAs(): tells the driver under which name to save the generated Word document
  • closeDocument(): tells the driver to close the generated Word document
  • printAndForget(): tells the driver to print the generated Word document to the default printer. An overloaded method allows the developer to hardcode the name of the printer to print to. The file is not saved anywhere!
  • printToPrinterToSelectByUserAndForget(): pops up the printer dialog and tells the driver to print the generated Word document to the chosen printer. The file is not saved anywhere!
  • executeMacro(): tells the driver to run the macro whose name is given in argument.

As you can see this Java2Word component is very simple, yet very powerful. Play around with it and you won't be disappointed. The installation is straightforward and the driver is very fast. In my case, I have to generate 50 pages Word files, and the whole thing takes like four to five seconds.

 
About this Blog