tags:

views:

2509

answers:

7

Hello,

I want to create a table in a Microsoft Office Word file using Java. Can anybody tell me how to do it with an example. Regards Prashanth Desai

A: 

I've never seen it done, and I work in Word a lot. If you really want to programatically do something in a word document then I'd advise using Microsoft's scripting language VBA which is specifically designed for this purpose. In fact, I'm working in it right now.

If you're working under Open Office then they have a very similar set of macro-powered tools for doing the same thing.

Kezzer
+3  A: 

Have a look at Apache POI

The POI project is the master project for developing pure Java ports of file formats based on Microsoft's OLE 2 Compound Document Format. OLE 2 Compound Document Format is used by Microsoft Office Documents, as well as by programs using MFC property sets to serialize their document objects.

eljenso
A: 

Office Writer would be a better tool to use than POI for your requirement.

If all you want is a simple table without too much of formatting, I would use this simple trick. Use Java to generate the table as HTML using plain old table,tr,td tags and copy the rendered HTML table into the word document ;)

rc
+1  A: 

Office 2003 has an xml format, and the default document format for office 2007 is xml (zipped). So you could just generate xml from java. If you open an existing document it's not too hard too see the xml required.

Alternatively, you could use openoffice's api to generate a document, and save it as a ms-word document.

Andrej
A: 

Our feature set is to hit a button in our web app and get the page you are looking at back as a Word document. We use the docx schema for description of documents and have a bunch of Java code on the server side which does the document creation and response back to our web client. The formatting itself is done with some compiled xsl-t's from within Java to translate from our own XML persistence tier.

The docx schema is pretty hard to understand. The way we made most progress was to create template docx's in Word with exactly the formatting that we needed but with bogus content. We then fooled around with them until we understood exactly what was going on. There is a huge amount in the docx that you don't really need to worry about. When reading / translating the docx Word is pretty tolerant to a partially complete formatting schema. In fact we chose to strip out pretty much all the formatting because it also means that the user's default formatting takes precedence, which they seem to prefer. It also makes the xsl process faster and the resulting document smaller.

Simon
A: 

Click here for a Working example with source code. This example generates MS-Word docs from Java, based on a template concept.

Cheeso
A: 

Further to Simon's reply, see http://dev.plutext.org/forums/viewtopic.php?f=6&t=7 re using docx4j to replace strings in the WordML representing your table (as created in Word itself).

plutext