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());
}
}
}