ASP.NET: How to export gridview data in excel sheet

The Basic algorithm behind the scene is,when i click to "Export to Excel file" button then GridView Data pass into excel sheet.So what steps can i take for pass data to excel sheet.
Step-1: First bind GridView control from DataSource.
Step-2: Take a one button control for passing data from GridView control to excell Sheet.
Step-3:  For passing GridView data to excel sheet then we must should take response object, Because if we want to get client computer cookies then we can take response object so here we use response object.
Step-4: Clear all content from response buffer using response object.

Response.ClearContent();

Step-5: Specify the excel file name using Add herader method of response object with content-disposition

Response.AddHeader("content-disposition", "attachment;filename=tarun.xls");


Step-6: The next thing to do set the content type using response object.

Response.ContentType = "application/excel";

Step-7:  Use two class first one is StringWriter class and second one is HtmlTextWriter class. Here StringWriter class available in System.IO namespace and inherited from TextWritter class.
Step-8:  Pass object of the StringWriter class to HtmlTextWriter class

   StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

Step-9 : Invoke RenderControl method of the GridView class.

GridView1.RenderControl(htw);

Step-10: Pass HtmlTextWriter class object to RenderControl method. Here HtmlTextWriter class object contain Text as well as Html content( table control).
Step-11 : Invoke Write method of Response object for writing text to the excel file.
Step-12 : Invoke End method of the Response object.

  Response.Write(sw);
        Response.End();
        
Step-13 : Handle "'GridView' must be placed inside a form tag with runat=server" using Override mwethod.

Complete code with output
Source code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="binding.aspx.cs" Inherits="binding" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="GetData"
        Width="123px" />
    <br />
    <div>
   
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
   
        <br />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click"
            Text="Export to excel file" />
   
    </div>
    </form>
</body>

</html>
Codebehind Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;

public partial class binding : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString =ConfigurationManager.ConnectionStrings ["ConnectionString"].ToString ();
        con.Open ();

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from deltable";
        cmd.Connection = con;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "deltable");
        GridView1.DataSource = ds;
        GridView1.DataBind();



    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition""attachment;filename=tarun.xls");
        Response.ContentType = "application/excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);
        Response.Write(sw);
        Response.End();
       
    }

   

}




Output
ASP:NET: How to export gridview data in excel sheet

2 comments:

  1. This is really cool article thanks....have a look of this article.
    http://www.dotnetpools.com/2012/09/gridview-export-to-excel-in-aspnet-c.html

    ReplyDelete