C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Exporting DataGrid to Excel in c#


Posted Date: 12 Aug 2008    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: D.Jeya kumar(JK)Member Level: Diamond    
Rating: Points: 10



Exporting DataGrid to Excel might not need any excel object model in asp.net application with datagrid. Below is the complete code to export the Datagrid value to Excel in C#.


Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";

System.IO.StringWriter swtext = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hwtext = new HtmlTextWriter(swtext);

DGexcel.RenderControl(hwtext);
Response.Write(swtext.ToString());
Response.End();



Response.AddHeader is to know that we are exporting to a file which is named FileName.xls.

Response.ContentType is the type of the file being exported.

DGexcel.RenderControl(hwtext) writes the data to the HtmlTextWriter.

Response.Write(swtext.ToString()) sends the request to the response stream.





Responses

Author: Raj Kumar Prajapati    27 Aug 2008Member Level: Bronze   Points : 0
GOOD
THANKS
RAJ PRAJAPATI


Author: Newman Joseph    28 Aug 2008Member Level: Silver   Points : 2
Use this ......

dg.DataSource=dsDetails;
dg.DataBind();
Context.Response.Buffer = true;
Context.Response.ClearContent();
Context.Response.ClearHeaders();
Context.Response.ContentType= "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=xxxxxx.xls");
dg.HeaderStyle.ForeColor= Color.Blue;
dg.HeaderStyle.BackColor = Color.White;
dg.ItemStyle.BackColor = Color.White;
EnableViewState=true ;
System.IO.StringWriter tw=new StringWriter();
System.Web.UI.HtmlTextWriter hw=new HtmlTextWriter(tw);
dg.RenderControl(hw);
Context.Response.Write(tw.ToString());
Context.Response.Flush();
Context.Response.Close();
Context.Response.End();


Regards,
Joseph



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Exporting DataGrid to Excel in c#  .  Exporting datagridin C#  .  Exporting datagrid  .  Export excel  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Editing, Deleting Gridview in VB.net 2.0
Previous Resource: Repopulate Grid-Using DropDownList
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use