Click here to Skip to main content
6,630,586 members and growing! (17,985 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Intermediate

MS Word 2003 XML Output

By Karthikg

How to give a Word output without MS Office Web Components (OWC) on the server.
C#, Windows, .NET, ASP.NET, Visual Studio, Dev
Posted:29 Feb 2004
Views:64,076
Bookmarked:42 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.45 Rating: 3.45 out of 5
1 vote, 10.0%
1
1 vote, 10.0%
2
2 votes, 20.0%
3
2 votes, 20.0%
4
4 votes, 40.0%
5

Perquisites

Word 2003 in client's PC.

Introduction

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. http://schemas.microsoft.com/office/word/2003/wordml 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 
{ 
    /// <summary> 

    /// Summary description for WebForm1. 

    /// </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) 
        { 
            InitializeComponent(); 
            base.OnInit(e); 
            // SQL Query 

            string sqlQry="SELECT CustomerID, CompanyName, " + 
                   "ContactName, City from customers"; 

            //Connection String 

            string connStr = "server=10.4.12.13;" + 
                   "database=northwind;user id=sa;password=aprimo"; 

            // Define Connection 

            SqlConnection sqlConn = new SqlConnection(connStr);

            // Open Connection 

            sqlConn.Open(); 

            // 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" + 
                      ".microsoft.com/office/word/2003/wordml\">"); 

            sb.Append("<w:body>"); 

            // Create Table and Column Headers 

            sb.Append<w:tbl>"); 
            sb.Append("<w:tr>"); 
            sb.Append("<w:tc><w:p><w:r><w:rPr>" + 
                "<w:b w:val=\"on\" /><w:t>"+
                dr.GetName(0).ToString()+
                "</w:t></w:rPr></w:r></w:p></w:tc>"); 
            sb.Append("<w:tc><w:p><w:r>" + 
                "<w:rPr><w:b w:val=\"on\" /><w:t>"+
                dr.GetName(1).ToString()+
                "</w:t></w:rPr></w:r></w:p></w:tc>"); 
            sb.Append("<w:tc><w:p><w:r><w:rPr>" + 
                "<w:b w:val=\"on\" /><w:t>"+
                dr.GetName(2).ToString()+
                "</w:t></w:rPr></w:r></w:p></w:tc>"); 
            sb.Append("<w:tc><w:p><w:r><w:rPr>" +
                "<w:b w:val=\"on\" /><w:t>"+
                dr.GetName(3).ToString()+
                "</w:t></w:rPr></w:r></w:p></w:tc>"); 

            sb.Append("</w:tr>"); 

            // Get Data 

            while (dr.Read()) 
            { 
                sb.Append("<w:tr>"); 
                sb.Append("<w:tc><w:p><w:r><w:t>"+
                   dr["CustomerID"].ToString()+
                   "</w:t></w:r></w:p></w:tc>"); 
                sb.Append("<w:tc><w:p><w:r><w:t>"+
                   dr["CompanyName"].ToString()+
                   "</w:t></w:r></w:p></w:tc>"); 
                sb.Append("<w:tc><w:p><w:r><w:t>"+
                   dr["ContactName"].ToString()+
                   "</w:t></w:r></w:p>/w:tc>"); 
                sb.Append("<w:tc><w:p><w:r><w:t>"+
                   dr["City"].ToString()+
                   "</w:t></w:r></w:p></w:tc>"); 
                sb.Append("</w:tr>"); 
            } 
            // sb.Append("</w:tr>"); 

            sb.Append("</w:tbl>");
            sb.Append("</w:body>"); 
            sb.Append("</w:wordDocument>"); 
            dr.Close();
            sqlConn.Close();
        } 

        /// <summary> 

        /// Required method for Designer support - do not modify 

        /// the contents of this method with the code editor. 

        /// </summary> 

        private void InitializeComponent() 
        { 
            this.Load += new System.EventHandler(this.Page_Load); 
        } 
        #endregion 

        private void Page_Load(object sender, System.EventArgs e) 
        { 
            // Set Mime Type 

            Response.ContentType = "application/msword"; 

            // Push the Data to the client. 

            Response.Write(sb.ToString()); 
        } 
    }
}

Conclusion

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.

License

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

Karthikg


Member
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.
Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
Questionrecord with & Pinmemberrodchar6:54 8 Dec '06  
AnswerRe: record with & PinmemberCharles E. Wagner Jr.9:44 5 Jan '07  
GeneralConversion of ASP to MS Word PinmemberVishnu_vardhan9:00 18 Jun '05  
GeneralThank you for available Code Pinmembernattapat0:00 7 Jun '05  
GeneralGreat work PinmemberVenkat Eswaran20:36 21 Dec '04  
GeneralOn a theme PinsussAnonymous0:16 27 Apr '04  
GeneralWhat about Word 2002 ? PinmemberBraulio Díez1:51 22 Mar '04  
GeneralRe: What about Word 2002 ? PinmemberKarthikg2:53 22 Mar '04  
GeneralInteresting, thanks PinmemberSteven Campbell12:41 1 Mar '04  
GeneralWord 2003 xml PinmemberStephane Rodriguez.23:07 29 Feb '04  
GeneralRe: Word 2003 xml PinmemberAbdul (Rajib) Bahar12:53 26 Apr '04  
GeneralMS Word View 2003 Pinmembermarkpers22:39 12 Jan '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Feb 2004
Editor: Smitha Vijayan
Copyright 2004 by Karthikg
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project