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 » Code Snippets » Application windows, menus & toolbars »

Export file to excel in ASP.NET C#


Posted Date: 08 Aug 2009    Resource Type: Code Snippets    Category: Application windows, menus & toolbars
Author: Dharmendra BanoliyaMember Level: Gold    
Rating: 1 out of 5Points: 8



Description :


In this article You can eaisly Export the data in 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;


Attachments

  • Export to excel (31198-1013-ExportDataToExcel.rar)

    Related Resources:
    Read articles related to Export to Excel
  • Responses

    Author: Neeraj Kumar SIngh    09 Aug 2009Member Level: Silver   Points : 1
    hello sir,
    nice code,but i m getting some error about rendering.
    the error is ::

    "RegisterForEventValidation can only be called during Render(); "


    please give me the complete running application of this code.

    thanks & regards,
    Neeraj Singh.


    Author: Neeraj Kumar SIngh    09 Aug 2009Member Level: Silver   Points : 1
    hello sir,
    nice code,but i m getting some error about rendering.
    the error is ::

    "RegisterForEventValidation can only be called during Render(); "


    please give me the complete running application of this code.

    thanks & regards,
    Neeraj Singh.


    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    InnerData is id of Div or table you use  .  In this article You can eaisly Export the data in 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: Title less Windows in WPF
    Previous Resource: WPF Button ToolTips
    Return to Discussion Resource Index
    Post New Resource
    Category: Application windows, menus & toolbars


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use