Thursday, January 15, 2015

Export ASP.NET web page into MS Word / How to export DIV tag contents into MS Word

I have been forced write this article “How to export ASP.NET Web Page content into MS Word” because of the huge number of similar questions I have been answering in Microsoft forums.

Sometimes you may have to export your webpage content into word or PDF or Excel especially if the user wanted to print the whole content of your webpage.

Today we will see this simple code.

First we will create a demo page for this purpose with few labels and textboxes.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExportToWord.aspx.cs" Inherits="DotnetGalaxy.ExportToWord" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Export To Word - Div Tag Content</title>
</head>
<body>
    <form id="form1" runat="server">
   <div id="ExportToWordDiv" runat="server">
        <table class="auto-style1" border="1">
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Address"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" Height="21px" Width="300px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Phone"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <asp:Button ID="btnExportToWord" runat="server" Text="Export To Word" OnClick="btnExportToWord_Click" />
                </td>
            </tr>
            
        </table>
    
    </div>
    </form>
</body>
</html>

As you can see in the above code, only difference from the normal code to the above one is I have given a name to the div tag, because we are going to export all the contents inside the DIV tag into MS Word.

Now we will run this project and see the result.

image

Its the time to write the code to export to Word now. Lets add the the required namespace first and then write the code under Button click event.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
namespace DotnetGalaxy
{
    public partial class ExportToWord : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
        protected void btnExportToWord_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=DemoExport.doc");
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.ContentType = "application/doc";
                System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                ExportToWordDiv.RenderControl(htmlWrite);
                Response.Write(stringWrite.ToString());
                Response.End();
            }
            catch (Exception ex)
            {
                string strError = "Error while exporting to Word" + ex.Message;
            }
        }
    }
}

Only one additional Namespace I have added and that is “using System.IO;” Don’t forget to add this namespace.

There is nothing else specific in the code to explain.

Lets see how this will be exported in MS Word.

image

As you can see the same content whatever it was there in the web page has been exported to MS Word.

I hope whatever I have explained above is very clear and no about about anything.

You are always welcome to ask questions or drop a comment about my article.

No comments: