Code to implement "Download as Word Document" feature for a web page with content from a data source

Web Development Tips, Tricks & Trivia


<%@ Page Language="C#" %>
<script runat="server">
    void Page_Load(Object Src, EventArgs E)
    {
        try
        {
            System.Data.DataTable workTable = new System.Data.DataTable();
            workTable.Columns.Add("Id");
            workTable.Columns.Add("Name");
            System.Data.DataRow workRow;

            for (int i = 0; i <= 9; i++)
            {
                workRow = workTable.NewRow();
                workRow[0] = i;
                workRow[1] = "CustName" + i.ToString();
                workTable.Rows.Add(workRow);
            }
            string strBody = DataTable2WordString(workTable);
            Response.AppendHeader("Content-Type", "application/msword");
            Response.AppendHeader("Content-disposition", "attachment; filename=my.doc");
            Response.Write(strBody);
        }
        catch(Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

//the style of this method was adapted from Daniel Olson's DataTable2ExcelString method 
//at http://authors.aspalliance.com/olson/methods/DataTable2ExcelString.aspx

    public string DataTable2WordString(System.Data.DataTable dt)
    {
    StringBuilder sbTop = new StringBuilder();

    sbTop.Append(@"<html " +  
            "xmlns:o='urn:schemas-microsoft-com:office:office' " + 
            "xmlns:w='urn:schemas-microsoft-com:office:word'" +  
            "xmlns='http://www.w3.org/TR/REC-html40'>" + 
            "<head><title>Time</title>");

    sbTop.Append(@"<!--[if gte mso 9]>" + 
                             "<xml>" +  
                             "<w:WordDocument>" + 
                             "<w:View>Print</w:View>" + 
                             "<w:Zoom>90</w:Zoom>" +  
                             "</w:WordDocument>" + 
                             "</xml>" +  
                             "<![endif]-->");

    sbTop.Append(@"<style>" + 
                            "<!-- /* Style Definitions */" + 
                            "@page Section1" + 
                            "   {size:8.5in 11.0in; " + 
                            "   margin:1.0in 1.25in 1.0in 1.25in ; " + 
                            "   mso-header-margin:.5in; " + 
                            "   mso-footer-margin:.5in; mso-paper-source:0;}" + 
                            " div.Section1" + 
                            "   {page:Section1;}" + 
                            "-->" + 
                           "</style></head>");

    sbTop.Append(@"<body lang=EN-US style='tab-interval:.5in'>" + 
                            "<div class=Section1>" + 
                            "<h1>Time and tide wait for none</h1>" +  
                            "<p style='color:red'><I>" + 
                            DateTime.Now + "</I></p></div><table border='1'>" ) ;

	string bottom = @"</table></body></html>";

        StringBuilder sb = new StringBuilder();
        //Header
        sb.Append("<tr>");
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            sb.Append("<td>" + dt.Columns[i].ColumnName + "</td>");
        }
        sb.Append("</tr>");

        //Items
        for (int x = 0; x < dt.Rows.Count; x++)
        {
            sb.Append("<tr>");
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                sb.Append("<td>" + dt.Rows[x][i] + "</td>");
            }
            sb.Append("</tr>");
        }

        string SSxml = sbTop.ToString() + sb.ToString() + bottom;

        return SSxml;
	}
</script>


Read related article in EggheadCafe.com
Back to landing page
Code formatted with Thomas Johansen's utility.