Saturday, July 6, 2013

How to Export Datalist Data into Word Document in Asp.net

Introduction: In this article I will explain how we can export the Datalist Data into Word Document in Asp.net.
Description:
Add a new webform to project. Drag and drop the controls from Toolbox. Here I bind the Datalist using Sqldatasource. As shown below:
<form id="form1" runat="server">
    <div>
    <asp:DataList ID="dlstudent" runat="server" DataKeyField="STUDENT_ID"
            DataSourceID="SqlDataSource1">
            <HeaderStyle Font-Bold="True" BorderColor="Black" />
            <HeaderTemplate>
             <table border="1"><tr style="background-color:Blue;color:White;">
             <td><b>Student Name</b> </td>
             <td>Student Address</td>
             <td>Student Class</td>
             </tr>
            </HeaderTemplate>
            <ItemTemplate>
            <tr style="font-style:italic;">
              <td align="center"><asp:Label ID="lblname" runat="server"
                    Text='<%# Eval("STUDENT_NAME") %>'></asp:Label></td>
                <td align="center"><asp:Label ID="lbladdress" runat="server"
                    Text='<%# Eval("STUDENT_ADDRESS") %>'></asp:Label></td>
               <td align="center"><asp:Label ID="lblclass" runat="server"
                    Text='<%# Eval("STUDENT_CLASS") %>'></asp:Label></td>                  
            </ItemTemplate>          
                       </asp:DataList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:TestBlogConnectionString %>"
            SelectCommand="SELECT * FROM [STUDENT_DETAIL]"></asp:SqlDataSource>
        <br />
    &nbsp;<asp:Button ID="Button2" runat="server" Text="Export To Word Document"
            onclick="Button2_Click" />
    </div>
    </form>

Now go to .aspx.cs page and write the below given code:

using System.IO;

protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            dlstudent.DataBind();
            Response.ClearContent();
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StudentDetails.doc"));
            Response.Charset = "";
            Response.ContentType = "application/ms-word";
            StringWriter writer = new StringWriter();
            HtmlTextWriter html = new HtmlTextWriter(writer);
            dlstudent.RenderControl(html);
            Response.Write(writer.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }

In VB (.aspx.vb)

Imports System.IO

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            dlstudent.DataBind()
            Response.ClearContent()
            Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "StudentDetails.doc"))
            Response.Charset = ""
            Response.ContentType = "application/ms-word"
            Dim writer As New StringWriter()
            Dim html As New HtmlTextWriter(writer)
            dlstudent.RenderControl(html)
            Response.Write(writer.ToString())
            Response.[End]()
        Catch ex As Exception
        End Try
    End Sub

Now build and run the project.

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment