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
{
public class WebForm1 : System.Web.UI.Page
{
StringBuilder sb = new StringBuilder();
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
string sqlQry="SELECT CustomerID, CompanyName, " +
"ContactName, City from customers";
string connStr = "server=10.4.12.13;" +
"database=northwind;user id=sa;password=aprimo";
SqlConnection sqlConn = new SqlConnection(connStr);
sqlConn.Open();
SqlCommand cmd = new SqlCommand(sqlQry,sqlConn);
SqlDataReader dr=null;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
sb.Append("<?xml version=\"1.0\"?>");
sb.Append("<w:wordDocument xmlns:w=\"http://schemas" +
".microsoft.com/office/word/2003/wordml\">");
sb.Append("<w:body>");
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>");
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:tbl>");
sb.Append("</w:body>");
sb.Append("</w:wordDocument>");
dr.Close();
sqlConn.Close();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Page_Load(object sender, System.EventArgs e)
{
Response.ContentType = "application/msword";
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.
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.