Export Gridview to excel using C#

This article will describe 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. Same code can be applied to any DataBound control to export its content to excel.

Follow below steps to export your Gridview content to excel.

  1. Create Gridview / repeater databound control

    Go through this article to create a Databound control with custom paging. It uses repeater control and show data with paging. You can use any databound control like Gridview with this code for exporting to excel. It uses Northwind database and Customers table to display data.
  2. Add Export to Excel button

    Add a new button with ID btnExportToExcel to the aspx page created in 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 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
  3. Export All Customers to Excel

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

    Add a button to the aspx page name it btnExportAllCustomer. In previous step we use the Data bound control which exist 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 on excel data formating.

Display this data in Crystal Report to add reporting functionality.

Download source code and required stored proc.



Latest Jobs in Seattle


Computer Software Engineers, Applications - ConnectiCare
ConnectiCare is looking to add an IT Solutions Developer to our Applications team in Farmington, Connecticut. The purpose of this position is to...
 
Computer Software Engineers, Applications - ConnectiCare
ConnectiCare is looking to add an IT Solutions Developer to our Applications team in Farmington, Connecticut. The purpose of this position is to...
 
Web Developers -
We are a privately owned mortgage bank, founded in 2007. Our staff utilize a state-of-the-art paperless technology which encompasses all departments...
 
Web Developers - Dovenmuehle Mortgage Inc.
This is a full- time position with Dovenmuehle Mortgage, Inc. located in Lake Zurich, IL POSITION SUMMARY: The Developer's role is to plan, design,...
 
Web Developers - Dovenmuehle Mortgage Inc.
This is a full- time position with Dovenmuehle Mortgage, Inc. located in Lake Zurich, IL POSITION SUMMARY: The Developer's role is to plan, design,...






Latest Blogs on Dotnetmentors.com


IIS settings to improve ASP.NET Website Performance
This article helps you to understand required IIS settings to improve ASP.NET Website Performance.
 
ASP.NET Website Performance Improvement Tips
This article give you some of the best tips to improve your ASP.NET Website performance.
 
Getting started with ASP.NET Web API
This article helps you to understand ASP.NET Web API concepts
 
Host ASP.NET Web API in IIS using Visual Studio Publish method
This article will help you to host your ASP.NET Web API in IIS using Visual Studio Publish method. You will use File System publish method for it.
 
REST based CRUD operations with ASP.NET Web API
This article will describe how to create restful CRUD (Create, Read, Update and Delete) operations on database using ASP.NET WEB API.


Speak your mind :
Leave a comment for this article on dotnetbloogers.com