Creating Word Documents by Using C# and XML DC

This article tells you how to create word documents in your application without using Office Interop or third-party libraries.

Local Companies

Intercontinental Computer Services
(202) 328-1788
1201 Pennsylvania Ave NW
Washington, DC
na
571-337-7104
1417 NST NW #403
washington, DC
Tekk Net Computer Services
(202) 529-9007
Washington
Washington, DC

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" ...


Read the Rest of this Article at Developer.com

Featured Local Company

na

571-337-7104
1417 NST NW #403
washington, DC


Topics: 
Architecture & Design Languages & Tools Project Management Web Services
Database Microsoft & .NET Security Wireless
Java Open Source Techniques XML