Creating Word Documents by Using C# and XML

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

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

Regional Articles
- Creating Word Documents by Using C# and XML Alabama
- Creating Word Documents by Using C# and XML Alaska
- Creating Word Documents by Using C# and XML Arizona
- Creating Word Documents by Using C# and XML Arkansas
- Creating Word Documents by Using C# and XML California
- Creating Word Documents by Using C# and XML Colorado
- Creating Word Documents by Using C# and XML Connecticut
- Creating Word Documents by Using C# and XML DC
- Creating Word Documents by Using C# and XML Delaware
- Creating Word Documents by Using C# and XML Florida
- Creating Word Documents by Using C# and XML Georgia
- Creating Word Documents by Using C# and XML Hawaii
- Creating Word Documents by Using C# and XML Idaho
- Creating Word Documents by Using C# and XML Illinois
- Creating Word Documents by Using C# and XML Indiana
- Creating Word Documents by Using C# and XML Iowa
- Creating Word Documents by Using C# and XML Kansas
- Creating Word Documents by Using C# and XML Kentucky
- Creating Word Documents by Using C# and XML Louisiana
- Creating Word Documents by Using C# and XML Maine
- Creating Word Documents by Using C# and XML Maryland
- Creating Word Documents by Using C# and XML Massachusetts
- Creating Word Documents by Using C# and XML Michigan
- Creating Word Documents by Using C# and XML Minnesota
- Creating Word Documents by Using C# and XML Mississippi
- Creating Word Documents by Using C# and XML Missouri
- Creating Word Documents by Using C# and XML Montana
- Creating Word Documents by Using C# and XML Nebraska
- Creating Word Documents by Using C# and XML Nevada
- Creating Word Documents by Using C# and XML New Hampshire
- Creating Word Documents by Using C# and XML New Jersey
- Creating Word Documents by Using C# and XML New Mexico
- Creating Word Documents by Using C# and XML New York
- Creating Word Documents by Using C# and XML North Carolina
- Creating Word Documents by Using C# and XML North Dakota
- Creating Word Documents by Using C# and XML Ohio
- Creating Word Documents by Using C# and XML Oklahoma
- Creating Word Documents by Using C# and XML Oregon
- Creating Word Documents by Using C# and XML Pennsylvania
- Creating Word Documents by Using C# and XML Rhode Island
- Creating Word Documents by Using C# and XML South Carolina
- Creating Word Documents by Using C# and XML South Dakota
- Creating Word Documents by Using C# and XML Tennessee
- Creating Word Documents by Using C# and XML Texas
- Creating Word Documents by Using C# and XML Utah
- Creating Word Documents by Using C# and XML Vermont
- Creating Word Documents by Using C# and XML Virginia
- Creating Word Documents by Using C# and XML Washington
- Creating Word Documents by Using C# and XML West Virginia
- Creating Word Documents by Using C# and XML Wisconsin
- Creating Word Documents by Using C# and XML Wyoming
Related Articles

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

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