views:

184

answers:

4

Hi,

anyone knows an easy method to generate mail merge .doc file from Java?

So, I want to create a Word (95/97) document in Word, put some simple placeholders in it (only single value, no iterators and other advanced tags) like the ones used with mailmerge option, and then at runtime replace those placeholders with values from Java.

One option is to use Jasperreports, but this would require that I create exact replica of non-trivial Word document in Jasper format, which is not easy and is hard to change later.

Is there some method of filling placeholders in Word from Java, which does not require low-level document alteration with positioning and others low-level .doc tags from code, but something like this:

docPreparer.fillPlaceholder('placeholder1', 'my real value from runtime');

Some CRMs do this via ActiveX control for internet explorer, and it works great (they use Word's mailmerge) but I need an all-Java solution.

Ideas?

Thanks,

Bozo

+1  A: 

Sound like you are looking for something like JODReports.
JODReports uses FreeMarker as template engine and OpenOffice.

All you have to do is have some templates in your document ie: ${something} and this will be replaced with whatever you want.

It also has some other extra features.

By using JODConverter 3.0.beta you can convert that openoffice document to anything openoffice supports, including Word 97 as you want. More information on JODConverter is here

We use these two fairly easily in our project.

Shervin
Did I got it right - in order for the above combination of JODReports/JODConverter to work, Open Office service must be running on the server?
bozo
Yes. However, you will only need to install openoffice "somewhere", and then you point JODConverter to your openoffice installation (either a server, or locally), and then it will work. Its only with the older version of JodConverter 2.x that you needed to have a service up and running.
Shervin
Do you use web service from JODConverter to generate doc, or you have server with open office service that listens on some remote host and set it up that way in the configuration of JODConverter?
bozo
our application runs on the same server as openoffice. So no need to do any remote host or webservice.However you whichever of those if that is preferable for you.
Shervin
A: 

I've also been looking for easy ways to construct .doc files, but not found one. Two solutions I've tried are:

  • Create an openoffice document (it's zipped XML, not too hard to reverse engineer), and then programmatically call openoffice to open that document, and save it in Word format.
  • If you can use the more recent .docx format (I know there is a converter for Word 2003, not sure about earlier versions), then you can create a docx file. There's a significant learning curve, but it's probably worth it if you're going to be interfacing with MS Office docs in the future. I've used (and built on) docx4j.

Here's how I would recommend learning about word .docx format (as mentioned in answer to this question)

  1. Write a very small document (a couple of lines) in Word, and save it as test.docx
  2. Rename test.docx to test.zip
  3. Unzip test.zip and look at the files that are inside.
  4. Be amazed at the complexity, depth, and unfriendly names of the XML elements.
  5. Perservere to try to understand the relationships between paragraphs (P), paragraph properties (PPr), Text, Runs, etc.
  6. Refer to the documentation (33MB zipped) to understand the finer details.
  7. Start writing code to create files in this format. Look at openxml4j as a tool for doing the 'packaging' -- putting together all those files with the .rels etc. You can also look at the source for some of the examples.
  8. If you download all the xsd schemas, you could use tools like Sun's jaxb or Apache's Xmlbeans to make nice java classes for P, PPr, R, RPr, etc.

If you can afford it, you might be interested in Aspose.

John
+2  A: 

The older .RTF file format can also be used. It is supported by all Word versions that I know. It is a plain text format, search and replacing substitution variables in it should not be a problem in any programming language.

You may rename your xyz.rtf file to xyz.doc. Word will open it without complaining and the users won't need to get used to a new file extension...

Miklos
A: 

Docmosis will do this for you - all Java (plus openoffice server-side) and platform independent. It relies on OpenOffice to do the reading and writing of various formats, but the population and document control are done by Docmosis. It's free if you don't need the scalability options and saves you writing the code to get OpenOffice to do it yourself.

jowierun