Click here to Skip to main content
Click here to Skip to main content

MS Word 2003 XML Output

, 29 Feb 2004
Rate this:
Please Sign up or sign in to vote.
How to give a Word output without MS Office Web Components (OWC) on the server.


Word 2003 in client's PC.


We generally give an HTML output to the browser to display results. Now we can give a Word output without MS Office Web Components (OWC) on the server.

Using the code

Any application, whether it is running on Microsoft or Java technologies, can stream out MS Word compatible XML. schema needs to be used. Users can use Microsoft or Java technologies to give an output, by just following the XML Schema Reference.

I am using Customers table in the Northwind database. Below is a code-behind code in C# for generating Word document in XML. Remove any HTML tag from the aspx page, only the Page directive should be there.

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web; 
using System.Text; 
using System.Web.SessionState; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 

namespace Word2003 
    /// <span class="code-SummaryComment"><summary> 
    /// Summary description for WebForm1. 
    /// <span class="code-SummaryComment"></summary> 
    public class WebForm1 : System.Web.UI.Page 
        // String Builder to hold the XML 
        StringBuilder sb = new StringBuilder(); 

        #region Web Form Designer generated code 
        override protected void OnInit(EventArgs e) 
            // SQL Query 
            string sqlQry="SELECT CustomerID, CompanyName, " + 
                   "ContactName, City from customers"; 

            //Connection String 
            string connStr = "server=;" + 
                   "database=northwind;user id=sa;password=aprimo"; 

            // Define Connection 
            SqlConnection sqlConn = new SqlConnection(connStr);

            // Open Connection 

            // Define SqlCommand 
            SqlCommand cmd = new SqlCommand(sqlQry,sqlConn); 

            // Define SqlDataReader 
            SqlDataReader dr=null; 

            // Execute Query and Populate Data Reader 
            dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 

            // XML Header. Schema is must. 
            sb.Append("<?xml version=\"1.0\"?>"); 
            sb.Append("<w:wordDocument xmlns:w=\"http://schemas" + 


            // Create Table and Column Headers 
            sb.Append("<w:tc><w:p><w:r><w:rPr>" + 
                "<w:b w:val=\"on\" /><w:t>"+
            sb.Append("<w:tc><w:p><w:r>" + 
                "<w:rPr><w:b w:val=\"on\" /><w:t>"+
            sb.Append("<w:tc><w:p><w:r><w:rPr>" + 
                "<w:b w:val=\"on\" /><w:t>"+
            sb.Append("<w:tc><w:p><w:r><w:rPr>" +
                "<w:b w:val=\"on\" /><w:t>"+


            // Get Data 
            while (dr.Read()) 


If Word 2003 is installed, then it will open up and display the data. If Word 2003 is not available on the client, XML will be displayed in the browser. Microsoft Office 2003 XML Reference Schemas are available for download here.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
India India
I am working as Technical Lead at Virtusa, Hyderabad, India. I have more than 6 yrs of experience on the Microsoft Web Platform. I have been working in and out DotNet Technology since Pre Beta Release. The articles are the outcome of the R&D I do in my freetime.

Comments and Discussions

Questionrecord with & Pinmemberrodchar8-Dec-06 6:54 
AnswerRe: record with & PinmemberCharles E. Wagner Jr.5-Jan-07 9:44 
GeneralConversion of ASP to MS Word PinmemberVishnu_vardhan18-Jun-05 9:00 
GeneralThank you for available Code Pinmembernattapat7-Jun-05 0:00 
GeneralGreat work PinmemberVenkat Eswaran21-Dec-04 20:36 
GeneralOn a theme PinsussAnonymous27-Apr-04 0:16 
QuestionWhat about Word 2002 ? PinmemberBraulio Díez22-Mar-04 1:51 
AnswerRe: What about Word 2002 ? PinmemberKarthikg22-Mar-04 2:53 
GeneralInteresting, thanks PinmemberSteven Campbell1-Mar-04 12:41 
GeneralWord 2003 xml PinmemberStephane Rodriguez.29-Feb-04 23:07 
GeneralRe: Word 2003 xml PinmemberAbdul (Rajib) Bahar26-Apr-04 12:53 
GeneralMS Word View 2003 Pinmembermarkpers12-Jan-05 22:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150302.1 | Last Updated 1 Mar 2004
Article Copyright 2004 by Karthikg
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid