Click here to Skip to main content
Click here to Skip to main content

Export Gridview Data to Excel in ASP.NET

, 17 Dec 2012
Rate this:
Please Sign up or sign in to vote.
This tip describes how to export data to an Excel file from a GridView.


This tip describes how to export data to an Excel file from a GridView. We will create a sample database and see how the data can be shown in the GridView and then export in Excel format.


There are many scenarios where we would want to export data into Excel format. The data could be from a GridView or from any other data bound control. In this article, we will see how we can export data from a GridView to Excel.

Using the Code

In this tip, I'm trying to explain how to create or export GridView to an MS-Excel file in ASP.NET using C#. I have also placed a button on the page for exporting data to a Microsoft Excel file. We have used a GridView which is bound and a button which will be used to create an Excel file.

For this, I have taken a database named demodb in which there is a table with the following columns:

Now when we have the table structure ready, we have a function for binding the Gridview for which I have used a Dataset. So let us look at the code that fetches data from the database and then binds the data to the grid to display it on the webpage.

protected void fillGrid()
    string str = "SELECT [UNo], [EmpName], [Age], 
    	convert(char,[dob],103) dob FROM [tbl_EmpDetails]";

    myConnection = new SqlConnection(conn);
    myCommand = new SqlCommand(str, myConnection);
    SqlDataAdapter mySQLDataAdapter;
    myDataSet = new DataTable();
    mySQLDataAdapter = new SqlDataAdapter(myCommand);
    GridView1.DataSource = myDataSet;
    ViewState["dtList"] = myDataSet;

protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)

Now, after binding a Gridview, data is ready to get exported to an Excel file. We click on a button named Export to Excel. I have used FileInfo to get the information related to the file.

FileInfo FI = new FileInfo(Path);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid DataGrd = new DataGrid();
DataGrd.DataSource = dt1;

string directory = Path.Substring(0, Path.LastIndexOf("\\"));// GetDirectory(Path);
if (!Directory.Exists(directory))

System.IO.StreamWriter vw = new System.IO.StreamWriter(Path, true);
WriteAttachment(FI.Name, "application/", stringWriter.ToString());

The above code uses a WriteAttachment function which pushes the attachment to the user in the Response object. The following code shows the implementation of WriteAttachment:

public static void WriteAttachment(string FileName, string FileType, string content)
    HttpResponse Response = System.Web.HttpContext.Current.Response;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
    Response.ContentType = FileType;

Point of Interest

In this tip, we have seen how to export data to an Excel file in ASP.NET. It is rather easy, but many new developers struggle with it so I wrote this to help them.


  • 16th October 2012: First version


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


About the Author

Software Developer
India India
I am a Software Engineer from Bhopal. I started my Career from Programming in ASP and now working as a Web Developer in ASP.Net (C#). I Love coding and always ready to gain new thing and always been towards Microsoft Technologies. Apart from coding my other hobbies are traveling, Internet Surfing, spending time with family and hang out with friends.
Follow on   Twitter   Google+

Comments and Discussions

QuestionThis is a wonderful component to export datatable or gridview to excel 2007/2010 files. PinmemberJack Smith17-Jun-14 20:09 
SuggestionMessage Automatically Removed PinmemberMember 862432517-Jun-14 1:24 
GeneralMy vote of 5 PinprofessionalDeepak Rana1119-Dec-13 0:50 
AnswerRe: My vote of 5 PinprofessionalAshishChaudha19-Dec-13 0:55 
QuestionOK PinprofessionalDeepak Rana1119-Dec-13 0:12 
QuestionMessage Automatically Removed PinmemberMember 1022174114-Oct-13 23:12 
GeneralMy vote of 4 PinmemberRachelle Villar22-Aug-13 20:58 
AnswerRe: My vote of 4 PinprofessionalAshishChaudha22-Aug-13 21:04 
QuestionPath does not work PinmemberManish Bhardwaj4-Jul-13 17:42 
QuestionMessage Removed PinmemberMember 87073803-Jun-13 10:17 
AnswerRe: Hi Code is working fine but not working on Server Pinmemberjonlink013-Jun-13 11:07 
AnswerRe: Hi Code is working fine but not working on Server PinprofessionalAshishChaudha3-Jun-13 17:49 
GeneralRe: Hi Code is working fine but not working on Server Pinmemberjonlink013-Jun-13 21:31 
AnswerRe: Hi Code is working fine but not working on Server PinprofessionalAshishChaudha3-Jun-13 22:51 
GeneralRe: Hi Code is working fine but not working on Server Pinmemberjonlink014-Jun-13 21:57 
GeneralRe: Hi Code is working fine but not working on Server PinprofessionalAshishChaudha4-Jun-13 23:58 
GeneralRe: Hi Code is working fine but not working on Server Pinmemberjonlink015-Jun-13 5:53 
GeneralMessage Removed PinmemberMember 87073805-Jun-13 8:14 
AnswerRe: Hi Code is working fine but not working on Server PinprofessionalAshishChaudha5-Jun-13 18:05 
Questionexporting grid view data to excel PinmemberMember 1004784720-May-13 23:50 
AnswerRe: exporting grid view data to excel PinprofessionalAshishChaudha21-May-13 21:27 
QuestionExport to Excel Pinmemberalexk1957199420-Feb-13 3:49 
QuestionMy vote of 5 Pinmemberbandi.ramesh15-Feb-13 22:04 
AnswerRe: My vote of 5 PinprofessionalAshishChaudha7-Apr-13 17:56 
GeneralMy vote of 5 Pinmemberbandi.ramesh15-Feb-13 0:48 
AnswerRe: My vote of 5 PinmemberAshishChaudha17-Feb-13 17:55 
GeneralMy vote of 5 PinmemberMitesh Machhi5-Feb-13 20:22 
AnswerRe: My vote of 5 PinmemberAshishChaudha6-Feb-13 7:12 
GeneralRe: My vote of 5 [modified] PinmemberJibesh12-Feb-13 10:14 
Questionerror occure PinmemberMember 81650405-Feb-13 0:02 
QuestionThanks Pinmemberalllaudhin22-Jan-13 0:51 
Questionexport gridview data to excel Pinmemberpriti shrivastava26-Dec-12 3:05 
AnswerRe: export gridview data to excel PinmemberAshishChaudha26-Dec-12 17:34 
AnswerRe: export gridview data to excel Pinmemberkarthikreddy08a5030-Jan-13 0:54 
GeneralMy vote of 5 Pinmemberopv12320-Dec-12 21:05 
AnswerRe: My vote of 5 PinmemberAshishChaudha20-Dec-12 21:37 
QuestionExporting GridView data to excel & PDF with sugession Pinmembervaraprasadreddy.bh19-Dec-12 7:04 
Questionadd some more columns Pinmemberkamil shaikh17-Dec-12 21:21 
AnswerRe: add some more columns PinmemberAshishChaudha17-Dec-12 21:36 
Suggestionanother similar article PinmemberLacy0016-Oct-12 22:18 
AnswerRe: another similar article PinmemberAshishChaudha23-Oct-12 0:16 
GeneralRe: another similar article Pinmemberbandi.ramesh15-Feb-13 22:02 
GeneralMy vote of 5 PinmemberMits Machhi16-Oct-12 18:07 
AnswerRe: My vote of 5 PinmemberAshishChaudha16-Oct-12 18:10 
AnswerRe: My vote of 5 PinprofessionalAshishChaudha21-May-13 21:28 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140902.1 | Last Updated 17 Dec 2012
Article Copyright 2012 by AshishChaudha
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid