1
votes

Does anyone have any experience with doing mail merge from Java on a word document? I need to support both doc and docx formats.

I have heard of Apache POI and docx4j. However, from reading around I'm sure how good the word support is in POI. docx4j only supports docx format as far as I can see.

Can any suggest either one of the above (and correct my knowledge on support) or another appropriate library. If necessary I would be willing to use one lib for doc and another for docx.

Thanks.

5

5 Answers

2
votes

Working with MS Word documents is very hard. DOC format is very complicated, DOCX is in theory simpler, but it is rather new and there is no mature, feature rich Java library to work with it and DOC as well.

The easiest solutions to most problems connected with working with MS Office documents is describe in the Joel Spolsky article. I hope you can use this method in your application.

2
votes

Docx4j has a MailMerger class. It works, but it strips out a lot of formatting in the process.

1
votes

Have a look at OpenOffice to see [manually] whether it can produce the types of files you need. Just load up some docs, and save some docs and docx versions. If the results pass these simple manual tests, then Docmosis or JODReports can automate this from Java.

1
votes

If you're open to non-free solutions, Aspose provides what appears to be a very sophisticated Java API for mail merging, among other things.

0
votes

I needed something similar and have built a simple project which allows to do a mail-merge of a word document and data from an Excel/CSV file.

You can find it at https://github.com/centic9/poi-mail-merge, maybe it already provides what you need here, otherwise it is hopefully easy to add more features.