Sunday, August 4, 2013

How to Export Gridview Data to Excel in Asp.net

Introduction: In this article I will explain how we can Export the Gridview Data to Excel in Asp.net
Description:
Take a new website. Add a webform to website and design .aspx page as shown below:
<body>
    <form id="form1" runat="server">
    <div>
     <table>
    <tr><td>&nbsp;</td><td align="right">
        <asp:Button ID="Button1" runat="server" Text="Export To Excel"
            onclick="Button1_Click" /></td></tr>
    <tr><td>&nbsp;</td><td> <asp:GridView ID="grdstudent" runat="server" AutoGenerateColumns="false" DataKeyNames="STUDENT_ID" DataSourceID="SqlDataSource1">
        <Columns>
        <asp:BoundField DataField="STUDENT_NAME" HeaderText="STUDENT NAME" />
        <asp:BoundField DataField="STUDENT_ADDRESS" HeaderText="STUDENT ADDRESS" />
        <asp:BoundField DataField="STUDENT_CLASS" HeaderText="STUDENT CLASS" />
        </Columns>
        </asp:GridView></td></tr>
    </table>
      
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:TestBlogConnectionString %>"
            SelectCommand="SELECT * FROM [STUDENT_DETAIL]"></asp:SqlDataSource>
    </div>
    </form>
</body>

Now go to .aspx.cs page and write the below given code:
public override void VerifyRenderingInServerForm(Control control)
    {
        // Verifies that the control is rendered
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            Response.ClearContent();
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StudentDetail.xls"));          
            Response.ContentType = "application/ms-excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            grdstudent.AllowPaging = false;
            grdstudent.DataBind();
            grdstudent.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }

In VB (.aspx.vb)

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        ' Verifies that the control is rendered
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Response.ClearContent()
            Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "StudentDetail.xls"))
            Response.ContentType = "application/ms-excel"
            Dim sw As New System.IO.StringWriter()
            Dim htw As New HtmlTextWriter(sw)
            grdstudent.AllowPaging = False
            grdstudent.DataBind()
            grdstudent.RenderControl(htw)
            Response.Write(sw.ToString())
            Response.[End]()
        Catch ex As Exception
        End Try

    End Sub


If you get any error "Control 'grdstudent' of type 'GridView' must be placed inside a form tag with runat=server" read this article:


http://articlemirror.blogspot.in/2013/07/control-grdstudent-of-type-gridview.html

No comments:

Post a Comment