Click here to Skip to main content
15,892,517 members
Articles / Web Development / ASP.NET
Tip/Trick

Export to Excel in ASP.NET C#

Rate me:
Please Sign up or sign in to vote.
4.84/5 (11 votes)
24 Feb 2014CPOL 85.7K   8   7
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.  

C++
  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();    }  <span style="font-size: 9pt;"> </span>
C++
public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */

            

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior) THOMSON REUTERS
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionExelente ! Pin
Daniel Gutierrez28-Mar-18 8:57
Daniel Gutierrez28-Mar-18 8:57 
AnswerRe: Exelente ! Pin
Nelek28-Mar-18 9:02
protectorNelek28-Mar-18 9:02 
Questionexport to excel Pin
Anand_@ndy29-Mar-17 19:44
Anand_@ndy29-Mar-17 19:44 
GeneralMy vote 3 Pin
Cosetter12-Mar-14 0:20
professionalCosetter12-Mar-14 0:20 
Hi, this is a nice tip, but note that this is a workaround and not a real excel export.
This statement "Export Gridview data in Excel format" is a bit misleading at least to me.
You could say "... in Excel compatible format", because you are generating a HTML file with an XLS extension.

As I sad this is a known workaround and it serves its purpose, but this can result in throwing an unfriendly warning when opening this file in some common Excel application because these applications are smart enough to detect that a file format and extension of this file do not match.

If you want to create a real XLS formatted file, try the following code:
C#
protected void btnexport_Click(object sender, EventArgs e)
{
    StringWriter StringWriter = new System.IO.StringWriter();
    HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
    gridstudentdetails.RenderControl(HtmlTextWriter);

    ExcelFile excel;
    var option = LoadOptions.HtmlDefault;
    using (var htmlStream = new System.IO.MemoryStream(option.Encoding.GetBytes(StringWriter.ToString())))
        excel = ExcelFile.Load(htmlStream, option);

    excel.Save(Response, "ExportData1.xls");
}

Code uses this .NET Excel component, which (as you can see from the code above) can convert a HTML to an XLS file in C#. The resulting "ExportData1.xls" uses a binary file format (BIFF8) which is used by real XLS extension files.
GeneralRe: My vote 3 Pin
JatinKhimani12-Mar-14 5:21
JatinKhimani12-Mar-14 5:21 
GeneralMy vote of 1 Pin
PBGuy24-Feb-14 18:10
professionalPBGuy24-Feb-14 18:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.