ASP.NET : GridView Data Export to Excel


 how to export gridview data to excel

HTML Markup : Design Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView.aspx.cs" Inherits="GridView" %>
<!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">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            HeaderStyle-CssClass="HeaderStyle" >
            <Columns>
                <asp:BoundField DataField="Book_Id" HeaderText="Book Id" 
                    SortExpression="Book_Id" />
                <asp:BoundField DataField="Book_name" HeaderText="Book Name" 
                    SortExpression="Book_name" />
                <asp:BoundField DataField="Book_author" HeaderText="Book Author" 
                    SortExpression="Book_author" />
                <asp:BoundField DataField="Book_Publisher_name" 
                    HeaderText="Book Publisher Name" SortExpression="Book_Publisher_name" />
                <asp:BoundField DataField="Book_Published_date" 
                    HeaderText="Book Published Date" SortExpression="Book_Published_date" />
            </Columns>            
        </asp:GridView>
        <br />
        <asp:Button ID="Btn_Export" runat="server" Text="Export to Excel" 
            onclick="Btn_Export_Click" />
    </div>
    </form>
</body>
</html>

CSS Code

<style type="text/css">
    #GridView1
     {
        background:#fafafa;               
     }
    #GridView1 .HeaderStyle
     {
         background-color:#0066cc;   
       color:#ffffff;
       padding:7px 10px 7px 10px;
     }
    #GridView1 th
     {
        padding:10px 15px 10px 15px;
     }
    #GridView1 td
     {
        padding:5px 10px 5px 10px;
        text-align:left;
     }    
   </style>

C# Coding

C# Coding : Namespace

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

C# Coding : Page Load

public partial class GridView : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["bookcon"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGridView();
        }
    }
}

C# Coding : Bind Gridview

    public void FillGridView()
    {
        DataSet ds = new DataSet();
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("Select * from Book", con);
        con.Close();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}

C# Code : Export Gridview Data to Excel

    protected void Btn_Export_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AppendHeader("content-disposition", "attachment; filename=Export_Gridview_to_Excel.xls");
        Response.ContentType = "application/excel";
        StringWriter stringwrite = new StringWriter();
        Html32TextWriter htmltextwriter = new Html32TextWriter(stringwrite);        
        GridView1.HeaderRow.Style.Add("background-color", "#0066cc");
        foreach (TableCell tbcell in GridView1.HeaderRow.Cells)
        {
            tbcell.Style["background-color"] = "#0066cc";
            tbcell.Style["color"] = "#ffffff";
            tbcell.Style["padding"] = "7px 15px 7px 15px";
        }
        foreach (GridViewRow gdrow in GridView1.Rows)
        {
            gdrow.BackColor = System.Drawing.Color.White;
            foreach (TableCell gdrowrbcell in gdrow.Cells)
            {
                gdrowrbcell.Style["background-color"] = "#ffffff";
            }
        }
        GridView1.RenderControl(htmltextwriter);
        Response.Write(stringwrite);
        Response.End();
    }
}
5 Komentar untuk "ASP.NET : GridView Data Export to Excel"

@piyush : its working for me right now..... try did u get any error

RegisterForEventValidation can only be called during Render();

I am getting this error

This comment has been removed by the author. - Hapus

The solution is quite simple you need to notify ASP.Net that not to validate the event by setting the EnableEventValidation flag to FALSE.

You can set it in the Web.Config in the following way

< pages enableEventValidation ="false" >< /pages >



This will apply to all the pages in your website. Else you can also set it in the @Page Directive of the page on which you are experiencing the above error.

< %@ Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false"

Back To Top