API for creating Word documents in Java

I would like to create a text document using a template, replace some variables (fields) and save it as a new word document.

I was thinking about using Apache POI, http://poi.apache.org/ , is this the best for this purpose? can you share your impression of this?

+7
java ms-word apache-poi
source share
8 answers

I have worked with POI before, and it is certainly capable of generating Word documents. But the devil is in the details.

There are thousands of functions in Word: you can put numbered lists starting at number 13, with negative indentation, into two connected cells of a table included in another table, which itself is part of the list of markers ... you get this idea. When the POI documentation says that they are under development, it reflects what is likely to be an eternal state in an attempt to catch up with the (for us, undocumented) Word specification.

Documents with a fairly "normal" set of functions used are well supported by POIs, whose interfaces and methods are reasonable and consistent, but sometimes require little work. But, as Pascal says, documents with a not-so-inconsistent feature set are also supported by RTF. I have almost no experience doing RTFs, but it's probably a little easier than working with POIs.

If you work in an environment or for a client who insists that your prepared documents be .DOC and not .RTF , then the POI is your only choice if you cannot enter the step in which you use the Office automation bit to converting RTF to DOC.

Update: I had a few more ideas.

Using a POI or creating RTF documents is something you could do on almost any platform. For example, at my work, all the servers that perform this processing work under Linux.

However, in the likely case when your programs run on Windows, there is another alternative: Jacob http://www.land-of-kain.de/docs/jacob/

Jacob is the COM interface for Java; it essentially allows you to “remotely control” Windows programs such as Word and Excel. The document that I linked to above does not apply to Jacob’s own site, but to one page with recipes for using Jacob’s culinary cutter. The project itself is located at SourceForge: http://sourceforge.net/projects/jacob-project/ But people say, and rightly so, that the documentation is a bit lacking.

Jacob has an edge over all the other solutions that you mean with the "real" Word, and therefore all the possibilities of Word are available to you. This would be an alternative if there were detailed aspects of your document that simply cannot be processed using the POI or the RTF format.

+8
source share

This is obviously too late, but since 2013 there is a much better and more flexible solution for creating a Word document.

http://www.docx4java.org/trac/docx4j

I had more luck with docx4j than ever with POIs.

+4
source share

If you use a template and do not want to create a text document from scratch, then what I know is POI is a pretty good solution. You open the template and select the zones you want to replace.

They say that POI is still evolving, but I use it in a production environment, and at the moment it works very well.

+3
source share

I'm not sure about the exact status of Word document support in the POI, but according to the POI website, the work is still ongoing (I can’t say exactly what this means). So, at this time, I would not use a POI, but tried to create an RTF document. To do this, you can:

  • Use the RTFTemplate , which is the RTF for the RTF Engine, which can generate an RTF document by merging the RTF model and data.
  • Use iText , which is a PDF generator at first, but can also create RTF .
  • Create your own solution (but I wouldn’t).

I would go for iText.

+2
source share

I know this question is a bit outdated, but I think that many people still find it using search engines, so I am posting another opportunity to do what you want here:

If the sole purpose is to have a Word template and replace some of the values ​​in it, you might consider saving the Word template as a single xml (not docx), and then process it in plain Java and without any Framework. If you want to do more (for example, create lists or tables), you can also think about understanding the xml format and write your own helpers before loading the Framework, such as a POI.

Here is an example of how to do this: http://dev-notes.com/code.php?q=10

This is a quick version, if you need a good version, you can try using an XML processor.

PS: users may notice that the file extension is not doc, but xml, and they can blame you for this, but this is normal ... just rename it to doc, the word recognizes the format and everyone will be happy again;)

+2
source share

You should learn the components of Aspose.Words. They recently started providing a Java version of the component.

See the following link: Aspose.Word for Java

It supports Word Automation, creation, and advanced features such as mail merge, without the need for an instance of Microsoft Word on the computer. The real benefits are that you can work in the context of a real Word document and not jeopardize the creation of RTF, etc.

Currently, the Java version is not a full-featured version of .Net, but the core functionality is there, and they are very advanced in order to get the equivalent version soon.

In addition, if you purchase a version of Java, you will receive free updates / support for several years after the creation of new releases.

+1
source share

If you are working with docx documents, the docx4j option is an option. Like a POI, its open source.

+1
source share

I created and used this: http://code.google.com/p/java2word

+1
source share

All Articles