Export gridview to Excel in Asp.net

 Gridview To Excel: This article explains how to export gridview to Excel file in Asp.net C#. When I was working on my previous projects, there is a requirement by one of my client that he wants to export data into MS Excel format.

The data could be from Asp.net GridView Control or any other data bound control or directly from code-behind DataTable. I have to export Asp.net GridView / Repeater control / DataTable data to Excel file using C#.

You can also check  Import Excel sheet data to Gridview in Asp.net C#, How to export gridview to pdf in asp.net c#.


Export Gridview to Excel in Asp.net C#

Methods for Export Data to Excel in Asp.net C#

  • By creating CSV file –  Have you ever open CSV file using EXCEL, if not then do open it once. What you are going to see it in Excel, is your CSV values are displayed as table-structured data .i.e comma separated value display as a Table cell form data. For creating Excel file in  C#, you can use this approach, only if you don’t want formatting, styles or workbook structure.
  • By creating XML file –  Excel allows you to open XML files with our without a schema. You can also see a table of data in Excel and can have some control on formatting if you use a schema.
  • By creating SYLK file –  Symbolic Link (SYLK) is a Microsoft file format typically used to exchange data between applications, specifically spreadsheets. SYLK files conventionally have a .slk suffix. Composed of only displayable ANSI characters, it can be easily created and processed by other applications, such as databases.

Using above any one method, we can export gridview data to excel file.

Step to Export Gridview to Excel.

  1. Add Gridview and Button control.
  2. Bind Gridview with data.
  3. Code to export data to excel.

# Html Markup: Add Asp.net Gridview control and button.

Open new visual studio project, add new Webpage (.aspx file) and now place an Asp.net Gridview control and a button control on newly added Webpage. So our page markup looks like as written below.

 # Code-behind: Bind Gridview control with data.

Here on code behind I have initialized a datatable and added some data to it.

And on page load event I have bind this datatable to my Asp.net Gridview control, you can also bind it with the database. So our code which shows data on gridview control looks like as written below.

# Code to export data to excel.

Yeah, now we are on the main part of this post i.e., code how to export gridview to excel in c#. We can do this by using any one of the above methods. Let’s first check by using Method 1, will update this with other methods later by this week

#Method 1: Create CSV file in Asp.net c#

For the most part, reading and writing CSV files are trivial. As the name suggestions, a CSV file is simply a plain-text file that contains one or more values per line, separated by commas.

Each value is a field (or column in a spreadsheet), and each line is a record (or row in a spreadsheet)

CSV files can easily be read and written by many programs, including Microsoft Excel. You can also check CSV-export tool for C#.

#Code: To generate CSV file from the Gridview data.

Here we make a loop over Asp.net Gridview control and get the cell value and then append it to StringBuilder variable. So our code looks like as written below

Note: Each comma represents a column.

Download Source Code

Output: Finally we are done with Excel File generation 🙂

Export Gridview to Excel in Asp.net C#

Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!

If You Liked It, could you do me a favour and tell your friends !! By sharing it on Facebook, Google+ or Twitter.

Get your Printable Copy