4
2010
Creating Word docx documents dynamically from Grails (or Java :-)
I’m presently working on a little side project that needs to generate a bunch of customisable client reports. One format of interest to our target market is the ubiquitous Word document. Now I know that Word switched to an zipped XML format a while back, so it’s given me a chance to do some researching into what Java libraries might exist for creating and editing the Word xml format.
I’ve played with Apache POI before, but it’s Word support for the latest gear is a little scratchy, so I figured I would hunt around for an nice libraries that people have built for handling the new XML format natively. I didn’t have to hunt far to discover the conveniently named docx4j.
This library gives you everything you need to create/load/edit/write Word docx documents from Java, and comes with a Maven repo, online Javadoc, and nice set of Sample code. What more could you ask for?
But I’m sure you’re just keen to see how to use it in your Grails app, right? Enough talk… First you’ll need to add a couple of entries to your /grails-app/conf/BuildConfig.groovy to make sure you pull in the dependencies. In the extract below, I’ve added a couple of new repos, and the dependency to pull in the docx4j jars:
repositories {
grailsPlugins()
grailsHome()
// uncomment the below to enable remote dependency resolution
// from public Maven repositories
mavenLocal()
mavenCentral()
// Details from docx4j Getting Started Guide at http://dev.plutext.org/svn/docx4j/trunk/docx4j/docs/Docx4j_GettingStarted.html
mavenRepo "http://dev.plutext.org/svn/docx4j/trunk/docx4j/m2"
mavenRepo "https://webdavclient4j.svn.sourceforge.net/svnroot/webdavclient4j/trunk/m2"
}
dependencies {
compile 'org.docx4j:docx4j:2.5.0'
}
With all our dependencies in place, we’ll just need to create some controller action that generates our Word document and sends it back to the client. I’m going to show one that queries my Asset class to pull back a set of assets and dump them in a Word doc:
import org.docx4j.openpackaging.packages.WordprocessingMLPackage
def exportWord = {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage()
def mainPart = wordMLPackage.getMainDocumentPart()
// create some styled heading...
mainPart.addStyledParagraphOfText("Title", "Asset Library")
mainPart.addStyledParagraphOfText("Subtitle", "Generated at " + Calendar.getInstance().getTime().toString())
// Add our list of assets to the document
Asset.list().each { asset ->
mainPart.addParagraphOfText(asset.name)
}
// write out our word doc to disk
File file = File.createTempFile("wordexport-", ".docx")
wordMLPackage.save file
// and send it all back to the browser
response.setHeader("Content-disposition", "attachment; filename=assets.docx");
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document")
response.outputStream << file.readBytes()
file.delete()
}
With our document sent, the user can fire it up from their browser window. Here's a sample of the output from above:

There are tons of things you can do with customising the documents in memory. I'll be doing some work importing Word templates, customising their fields, then exporting the finished product to the browser. Fun times ahead!
Enjoy your docx generating!
4 Comments + Add Comment
Leave a comment
Glen Smith
Archives
- January 2012
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- April 2011
- March 2011
- January 2011
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- September 2004
- August 2004
- July 2004
- June 2004
- May 2004
- March 2004
- February 2004
- January 2004
- December 2003
- November 2003
- October 2003
- September 2003

An article by Glen





Where are the styles that are applied to “Title”, “Subtitle”, etc. defined, within the library?
Hi Donal, I think the styles are just the built in Word styles.
They are not part of the Java library itself – Word just renders the named styled text in whatever that style is set to in Word.
@Donal, @Glen Smith
Hi!
The styles are the built-in styles available in MS Word, but you can define your own styles too. “Normal” is the default and apart from “Title” and “Subtitle”, you can also find “Heading 1″, “Heading 2″, … for headings.
Cheers Glen, I too need to make the change from doc to docx, this library looks sweet. I’ve only used POI for .xls and with a few wrappers is nice enough but his looks great!