C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Export to Excel


Posted Date: 11 Aug 2009    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: kinjalMember Level: Bronze    
Rating: 1 out of 5Points: 5



Export to Excel

private void ExportGridView()
{
string attachment = "attachment; filename=fileName.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
//Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
//grd_InitiatedDayCase.Parent.Controls.Add(frm);
innerData.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
//frm.Controls.Add(grd_InitiatedDayCase);
frm.Controls.Add(innerData);
frm.RenderControl(htw);
//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}


* innerData is id of Div or table you use.
=====================================================

protected void btn_ExportToExcel_Click(object sender, EventArgs e)
{
ExportGridView();
}


where btn_ExportToExcel is either LinkButton or Button
======================================================

< asp:LinkButton ID="btn_ExportToExcel" runat="server" OnClick="btn_ExportToExcel_Click" > Export To Excel < /asp:LinkButton >


Here we use the link Button to export file in the excel.
==========================================================
Note:

< div id="innerData" runat="server" >
put the control here whose data you want to show
< /div >

==========================================================
Namespace you need to use:

using System.IO;


Related Resources:
Read articles related to Export to Excel

Responses

Author: Filip    20 Aug 2009Member Level: Silver   Points : 2
Nice try but using html to simulate excel file is not best thing to do. You could try using GemBox .NET Excel component. Here is an example how you can export to excel with this component.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Export to excel  .  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: AJAX and XML HTTP Request
Previous Resource: Water Mark for Textbox
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use