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.ContentType = "applicatio/excel";
            StringWriter sw = new StringWriter(); ;
            HtmlTextWriter htm = new HtmlTextWriter(sw);

    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.ContentType = "applicatio/excel";
            StringWriter sw = new StringWriter(); 
            HtmlTextWriter htm = new HtmlTextWriter(sw);
            Repeater repAllCustomers = this.repCustomers;
            repAllCustomers.DataSource = 
    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.

Jobs in Mumbai

Web Developers - AAVID CODE-1
written English. Proficient in customizing and developing websites in WordPress, Joomla, Drupal CMS. SQL ...
Web Developers - Cyberindigo Net Pvt Ltd
Expertise in ASP.Net development Expertise in C#, VB.net Ease with Javascript and VB Script ... ASP.Net,C#.net,Ajax is required. 3. Understanding of...
Web Developers - C-MOTS Internet Technologies Pvt. Ltd.
The candidate should be an expert web technologies (PHP, mySQL, JavaScript, AJAX, HTML, CSS). Preference shall be given to candidates with the...
Web Developers - Basil Life Care Pvt Ltd.
C#/VB.Net, Asp.Net SQL Server Crystal Reports Silverlight Knowledge of Cloud Computing will be ...
Web Developers - eXEGESIS Infotech (I) Pvt LTd
1. Should have strong experience on ASP.net, VB.net/C# and SQL Server. 2. Should have worked ...

Latest Blogs on Dotnetmentors.com

ASP.NET MVC Model Example
This article describes you about what is ASP.NET Model, example of how to use and bind model data, how to make GET or Post request using model.
Getting Started Tutorial with ASP.NET MVC
This article gives you step by step tutorial to create first ASP.NET MVC application using feature like Controller and View.
ASP.NET MVC Controller, Action Method and ActionResult
n this article I am going to explain about ASP.NET MVC Controller, its Action Method and Action Result. And how to create and use controller in MVC application
Hello World Example With AngularJS and ASP.NET MVC
This article equips you to create your first hello world example using AngularJS framework and ASP.NET MVC.
How to Use Areas in ASP.NET MVC
This article describe about Areas in ASP.NET MVC application and organize application structure in better and simple way.

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