Export Gridview Content to Excel using C#

This article will describe to you how to export Asp.net GridView / Repeater control data to excel using C#. We will use the Gridview / Repeater data bound controls created in previous articles. The same code can be applied to any DataBound control to export its content to excel.

Create Gridview / repeater databound control

Go through this article to create a Databound control with custom paging. It uses repeater control and shows data with paging. You can use any databound control like Gridview with this code for exporting to excel. It uses the Northwind database and the Customer table to display data.

Add Export to Excel button

Add a new button with ID btnExportToExcel to the aspx page created in the previous step. On click of this button Customers shown for current PageIndex of Gridview or Repeater will be export to Excel.

For exporting to excel we will render GridView or Repeater control to HtmlTextWriter and string return by HTMLTextWriter will be written to the page by Response object.

Add below code for btnExportToExcel click event.

    
protected void btnExcel_Click(object sender, EventArgs e)
{
    Response.ClearContent();
    Response.AddHeader("content-disposition", 
        "attachment;filename=Customers.xls");
    Response.ContentType = "applicatio/excel";
    StringWriter sw = new StringWriter(); ;
    HtmlTextWriter htm = new HtmlTextWriter(sw);
    repCustomers.RenderControl(htm);
    Response.Write(sw.ToString());
    Response.End();
}
            
        

Export Customers to Excel

Export All Customers to Excel

In the previous step, we have written code to export in Excel for GridView or Repeater data which is displayed on the current page. Now we will see How to export all customers to Excel.

Add a button to the aspx page name it btnExportAllCustomer. In the previous step we have used the Data bound control which exists on page.

For exporting all customers we need to create a Gridview or Repeater similar to the existing control runtime using C# and render it using HtmlTextWriter

Add below code for btnExportAllCustomer click event

    
protected void btnExportAllCustomer_Click(object sender, EventArgs e)
{
    Response.ClearContent();
    Response.AddHeader("content-disposition",
            "attachment;filename=Customers.xls");
    Response.ContentType = "applicatio/excel";
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter htm = new HtmlTextWriter(sw);

    Repeater repAllCustomers = this.repCustomers;
    repAllCustomers.DataSource = 
        CustomerService.GetAllCustomers(0,
                CustomerService.GetCustomerCount());
    repAllCustomers.DataBind();
    repAllCustomers.RenderControl(htm);

    Response.Write(sw.ToString());
    Response.End();
}
            
        

Notice that we have created repAllCustomers runtime like repCustomers which is existing Repeater control. Bind it to all customers and render it.

Export all customers to Excel

This will convert current page HTML to excel however it is difficult to format data. You can use Microsoft.Office.Interop.Excel to get more control over excel data formating.

Display this data in Crystal Report to add reporting functionality.

Source code on Git hub Source Code on Github

Speak your mind
Please login to post your comment!


  • geeksarray user
    05/24/2016 01:38 PM FerozKhan

    Response its not founding how i can found what kind of dll i need to import in refrence or any other way please help about this.. thanks.

  • geeksarray user
    05/25/2016 09:40 PM Laxmikant

    check for the System.Web.UI namespace

  • geeksarray user
    06/20/2016 02:41 AM Jerwin

    hi, i try this and copy all your codes, but what i am getting is instead of .xls file it's .aspx file?

Blog Search





If you like my content please feel free to buy me coffee. Buy Me A Coffee