How to export Data List to excel in asp.net

No.of Views1404
Bookmarked0 times
Downloads 
Votes0
By  Mostafa Elzoghbi   On  09 Jun 2010 08:06:44
Tag : ASP.NET , Grid Controls
this codesnippet is very tricky because it shows a trick about exporting datalist data to excel.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

This codesnippet is very tricky because it shows a trick about exporting datalist data to excel.if you have a dtagrid and you want to export its data to excel,by few lines of code you can export its data to an excel file.

 

Response.Clear();Response.Buffer= true;Response.ContentType = "application/vnd.ms-excel";Response.Charset = "";this.EnableViewState = false;System.IO.StringWriter oStringWriter = new System.IO.StringWriter();System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);this.ClearControls(DataGridView);DataGridView.RenderControl(oHtmlTextWriter);Response.Write(oStringWriter.ToString());Response.Flush();Response.End();

the important thing here,is to export a datalist which contains a composite controls like a datagrid inside a datalist this code will not work,simply to solve the problem,comment this line of code in export function :
  

//this.ClearControls(DataGridView);

then simply all child controls in the datalist will be export to Excel.

i hope this is help and save your time.

 
Sign Up to vote for this article
 
About Author
 
Mostafa Elzoghbi
Occupation-
Company-
Member Type-Junior
Location-United States
Joined date-07 Jun 2010
Home Page-http://moustafa-arafa.blogspot.com
Blog Page-http://moustafa-arafa.blogspot.com
MCSD,MCT,MCTS,C# MVP
 
 
Other popularSectionarticles
    This CodeSnippet can be used to add and handle the checkboxes in GridView
    Published Date : 14/Feb/2011
    In many situations we may need to add auto number or serial numbers in grid view column, but we cannot find a property in property window to add this.
    Published Date : 26/May/2010
    In this article I am going to give you a brief explanation regarding how to use a Radio button in grid view. Actually I am going to show how we can delete a row from grid view by selecting the radio button inside the grid view and by clicking on delete button outside the grid view
    Published Date : 15/May/2010
    In this codesnippet, i will show How to Delete Row in GridView using JQuery in ASP.NET.
    Published Date : 20/Jul/2011
    In this snippet, I will show how to format a cell and apply style in gridview using JQuery. Sometimes we may need to apply the format for a particular cell based on the cell value; it can be done in within DataRowBound event in asp.net.
    Published Date : 05/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top