How to download data from Grid view to Excel Sheet

April 29, 2013 |

 Charlieform.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Charlieform.aspx.cs" Inherits="Charlieform"
EnableEventValidation="false" %>

<!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 id="Head1" runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        .style1
        {
            width: 1212px;
        }
        .style2
        {
            width: 1019px;
        }
        .style3
        {
            width: 1084px;
        }
        .style5
        {
        }
        .style6
        {
            width: 128px;
            height: 30px;
        }
        .style7
        {
            width: 1084px;
            height: 30px;
        }
        .style8
        {
            height: 30px;
        }
        .style9
        {
            width: 128px;
            height: 33px;
        }
        .style10
        {
            width: 1084px;
            height: 33px;
        }
        .style11
        {
            height: 33px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    <table class="style1">
        <tr>
            <td align="center" class="style5">
                &nbsp;</td>
            <td align="center" colspan="2">
                <asp:Label ID="Label1" runat="server" Text="Student Details"
                    Font-Size="X-Large"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="center" class="style5" colspan="3">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style5">
               
                    &nbsp;</td>
            <td class="style2" colspan="2">
               
                    <asp:GridView ID="gvdetails" runat="server" AllowPaging="true"
                        AllowSorting="true" AutoGenerateColumns="false" DataSourceID="dsdetails"
                        Height="33px" Width="1046px">
                        <RowStyle BackColor="#EFF3FB" />
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:BoundField DataField="Stud_Name" HeaderText="Name"
                                SortExpression="Stud_Name" />
                            <asp:BoundField DataField="Stud_Year" HeaderText="Year"
                                SortExpression="Stud_Year" />
                            <asp:BoundField DataField="Stud_Address" HeaderText="Address"
                                SortExpression="Stud_Address" />
                            <asp:BoundField DataField="Stud_City" HeaderText="City"
                                SortExpression="Stud_City" />
                            <asp:BoundField DataField="Stud_State" HeaderText="State"
                                SortExpression="Stud_State" />
                            <asp:BoundField DataField="Stud_Country" HeaderText="Country"
                                SortExpression="Stud_Country" />
                            <asp:BoundField DataField="Stud_Email" HeaderText="Email"
                                SortExpression="Stud_Email" />
                            <asp:BoundField DataField="Hostel" HeaderText="Hostel"
                                SortExpression="Hostel" />
                            <asp:BoundField DataField="Stud_Phone" HeaderText="Phone"
                                SortExpression="Stud_Phone" />
                            <asp:BoundField DataField="Entry_Date" HeaderText="Entry Date"
                                SortExpression="Entry_Date" />
                            <asp:BoundField DataField="Exit_Date" HeaderText="Exit Date"
                                SortExpression="Exit_Date" />
                            <asp:BoundField DataField="Stud_Room" HeaderText="Room"
                                SortExpression="Stud_Room" />
                            <asp:BoundField DataField="F_EID" HeaderText="Parents EID"
                                SortExpression="F_EID" />
                                <asp:BoundField DataField="FPH" HeaderText="Parents PH"
                                SortExpression="FPH" />
                        </Columns>
                    </asp:GridView>
                </td>
        </tr>
        <tr>
            <td class="style6">
                </td>
            <td class="style7">
                <asp:SqlDataSource ID="dsdetails" runat="server"
                    ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\data.mdf;Integrated Security=True;User Instance=True"
                    ProviderName="System.Data.SqlClient"
                    SelectCommand="select * from info where Hostel='ct'" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
            <td class="style8">
                </td>
        </tr>
        <tr>
            <td class="style9">
                </td>
            <td class="style10">
                &nbsp;&nbsp;
                <asp:Label ID="Label2" runat="server" Text="DOWNLOAD"
                    Font-Size="X-Large"></asp:Label>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:ImageButton ID="btnExcel" runat="server" ImageUrl="~/img/index.jpg"
                    onclick="btnExcel_Click" Height="33px" Width="44px" />
            </td>
            <td class="style11">
                </td>
        </tr>
        <tr>
            <td class="style6">
                &nbsp;</td>
            <td class="style7">
                &nbsp;</td>
            <td class="style8">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style5">
                &nbsp;</td>
            <td class="style3">
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Back"
                    Width="151px" />
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style5">
                &nbsp;</td>
            <td class="style3">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
   
</asp:Content>

    </div>
    </form>
</body>
</html>









Charlieform.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;
using System.Design;
using System.Drawing;

public partial class Charlieform : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
  
    protected void btnExcel_Click(object sender, ImageClickEventArgs e)
    {
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StudentInfo.xls"));
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        gvdetails.AllowPaging = false;
        gvdetails.DataBind();
        //Change the Header Row back to white color
        gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
        //Applying stlye to gridview header cells
        for (int i = 0; i < gvdetails.HeaderRow.Cells.Count; i++)
        {
            gvdetails.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
        }
        int j = 1;
        //This loop is used to apply stlye to cells based on particular row
        foreach (GridViewRow gvrow in gvdetails.Rows)
        {
            gvrow.BackColor = Color.White;
            if (j <= gvdetails.Rows.Count)
            {
                if (j % 2 != 0)
                {
                    for (int k = 0; k < gvrow.Cells.Count; k++)
                    {
                        gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                    }
                }
            }
            j++;
        }
        gvdetails.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("charlie.aspx");
    }
    protected void gvdetails_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

Asp.Net Examples: DataPager Fields ButtonCssClass - how to use ButtonCssClass in DataPager