Export Gridview to Excel asp.net 2.0

,
It is easy to make excel file of gridview just copy and paste the following code to your csharp code and change the gridview name. It will generate the excel sheet easily. please don't forget to give comments

protected void Button1_Click(object sender, EventArgs e)
{
HtmlForm form = new HtmlForm();
string attachment = "attachment; filename=Employee.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
form.Controls.Add(gvContact);
this.Controls.Add(form);
form.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
}

0 comments:

Post a Comment