65.9K
CodeProject is changing. Read more.
Home

Export to Excel in ASP.NET C#

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.84/5 (10 votes)

Feb 24, 2014

CPOL
viewsIcon

86682

Export Gridview data in Excel formate with gridview design.

Introduction

Export Gridview data to Excel formate and provide download that data.

Background 

That provide the your Gridview control data and DataGrid control data into Excel formate with rows and column formate. its also useful to make your own custome design and then export it.

Using the code

Provide very sort and efficient solution of export your data which are display in gridview and datagrid control are exported into excel fomate.

For implementation try to follow below steps..

Step-1   Go your page source and write EnableEventValidation="false" .

             <%@ Page Title="" Language="C#" EnableEventValidation="false"                  MasterPageFile="~/Admin/AdminMaster.master"  AutoEventWireup="true" odeFile="Admin_TotalStudents.aspx.cs" Inherits="Admin_Admin_TotalStudents" %>

 Step-2   Then add below code in button click event.  

  protected void btnexport_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=ExportData1.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";
        StringWriter StringWriter = new System.IO.StringWriter();
        HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
        
        gridstudentdetails.RenderControl(HtmlTextWriter);
        Response.Write(StringWriter.ToString());
        Response.End();    }   
public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }