I recently did a bit of work on generating Word 2007 documents using OpenXML. Basically, I had a standard XML document and I needed to generate a Word document from it. The process I came up with involved creating a "template" document in Word, examining the contents of the package, using XSLTs to transform the original XML into OpenXML and packaging it into the template. This actually seemed to work quite well and was far more efficient than using Office Interop for server-side document generation.
I've put together a few of videos describing the end-to-end process of examining a Word 2007 document package, generating OpenXML and using the System.IO.Packaging namespace. They are currently available as downloads from my website, but if there's enough demand I'll re-jig them and put them up on youtube. Here are the links:
Generating OpenXML Documents Part 1
Generating OpenXML Documents Part 2
Generating OpenXML Documents Part 3
If you've seen OpenXML before and you just want to get into the meat of how to use the Packaging namespace, then feel free skip the first two and go straight for part 3.
So generally, here's how it all works:
- Create a new Word 2007 document and format it according to what you'd like the generated document to look like.
- Pull apart the document "package" by simply changing the extension to zip and extracting it.
- Examine the parts of the package that you need to modify to generate the document. This is usually the "word/document.xml" file, but can also include files like "word/_rels/document.xml.rels" if you are adding relationships such as images to the document.
- Create an XSLT to transform your original XML data into the OpenXML represented by "word/document.xml".
- Use the System.IO.Packaging namespace to package the transformed XML and any images into (a copy of) the template file.
Let me know what you think... :)