Take the Pain out of Creating Word Documents by Using C# and XML
In today's web applications, quite often you run into the need to create a Word document that user can download. The two most common ways of accomplish this is to install a copy of Word with Office Interop on the server or use a third-party library. Both of these options have significant downsides; however, there is a far simpler way to create Word Documents using XML. Because Word natively has the ability to read/write Word documents in XML, you can utilize all of the XML/XSLT tools in .NET to create them.
The first step is to create a normal Word document to be used as a template. Figure 1 shows a simple Word doc with which you will start to produce a report for Sales Territories from the Adventure Works sample database.
Figure 1: Word Document Template
Although this document does not make much sense at this point, you will use the markers in the document to help build the XSLT. To save this document to an XML file, you will need to save the file in the Word 2003 XML Document format shown in Figure 2.
Figure 2: Save as Word XML
Creating the XSLT
Now that you have an XML file, you can start the process of creating an XSLT (EXtensible Stylesheet Language Transformation). First, change the extension of the file from XML to XSLT. Then, open the XSLT in an XML editing tool. Next, you need to add a couple of tags at the top and bottom so that you can call this a XSLT. At this point, the top of the XSLT document should show the following couple of tags:
<?xml version="1.0" encoding="utf-8"?> <w:wordDocument xmlns:aml= "http://schemas.microsoft.com/aml/2001/core" ...
You need to add two lines between these, as shown below:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">
Then, at the end of the file, you need to add the following two lines to close out those tags you added to the top:
Now, you have a very basic XSLT; however, you have not really done anything interesting with it yet. Before you go further into the XSLT, you need to take a look at the XML you are going to create to build your Word doc.
<?xml version="1.0" encoding="utf-8"?> <doc> <title>The Title of my Document</title> <region> <name>Region #1</name> <salesdiff>$123,000</salesdiff> <group> <group> <groupname>Group #1</groupname> <salesdiff>$300</salesdiff> </group> </group> <group> <group> <groupname>Group #2</groupname> <salesdiff>$300</salesdiff> </group> </group> </region> <region> <group> <group> <groupname>Group #1</groupname> <salesdiff>$300</salesdiff> </group> </group> <group> <groupname>Group #2</groupname> <salesdiff>$300</salesdiff> </group> </region> </doc>
This XML file contains two regions; within each region are two groups. Now that you have a sample XML file, you have a reference to use in adding to the XSLT. Starting with the document title, search the XSLT for the text "Document Title". Then, replace "Document Title" with <xsl:value-of select="doc/title"/>. After modifying the line in the XML, it should look the same as the following: