Click here to Skip to main content
12,357,252 members (74,617 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.3K views
2 bookmarked
Posted

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

Introduction

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.

Background

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.ClearContent();
              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);
              Repeater2.RenderControl(htm);
              Response.Write(sw.ToString());
              Response.End();
          }
      }

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

Happy coding! Smile | :)

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.

History

  • 12th May, 2014: Initial post

License

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

Share

About the Author

Tejal S
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
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.

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