Click here to Skip to main content
12,884,771 members (31,874 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 12 May 2014

Export Data from Repeater/Datagrid to Excel

, 12 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Export data from Repeater/Datagrid to Excel


This tip will give you a summary of how to export data from a Repeater to Excel sheet. One can very well use the same to import the same from Datagrid.


You need to have basic knowledge of creating and binding a repeater / datagrid.

Using the Code

Place a button on the web page from which the user is just click away to download the data to Excel sheet.

Your aspx code will be like below.

Specify an OnClick function for the button. Specify a name to your OnClick event.

In this case, it is Excel_Click.

NOTE: The OnClick function will trigger when the user clicks on the below button.

<asp:Button ID="btnExporttoExcel" runat="server" Text="Export to Excel" OnClick="Excel_Click"/>  

You now need to define the above event in your code behind file.

For the below code, instead of Repeater2, specify your Repeater ID.

Please note here in order to write the data from repeater to Excel, the Repeater needs to be in visible mode.

The code will look like below:

protected void Excel_Click(object sender, EventArgs e)
              Response.Buffer = true;
              Response.AddHeader("content-disposition", "attachment;filename=Details.xls");
              Response.Charset = "";
              Response.ContentType = "application/excel";
              System.IO.StringWriter sw = new System.IO.StringWriter();
              HtmlTextWriter htm = new HtmlTextWriter(sw);

Your data is now ready to be downloaded in an Excel sheet.

Happy coding! :)

Points of Interest

In case the repeater you are trying to bind is in Invisible mode, then on the button click, make it visible and at the end of the code, set the visibility to false. You can use the same for Datagrid as well.


  • 12th May, 2014: Initial post


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


About the Author

Tejal S
Software Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionIssue while downloading huge data Pin
Member 103721553-Oct-16 0:21
memberMember 103721553-Oct-16 0:21 
Questionexport excel. Pin
Member 1108559312-Aug-15 20:22
memberMember 1108559312-Aug-15 20:22 
QuestionNice Tejal S! Pin
Volynsky Alex12-May-14 8:06
professionalVolynsky Alex12-May-14 8:06 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 12 May 2014
Article Copyright 2014 by Tejal S
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid