Just paste this code in a button click Event. it will work
{
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
GridView1.DataSource = DT // your data table
GridView1.DataBind();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=My Report.Xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
string style = "<style> td{mso-number-format:\\@}</style>";
Response.Write(style);
Response.Write("<Center > <font size=2 face=Verdana><b>My Report </b></font> </Center> ");
Response.Write("<table><tr></tr></table>");
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.Flush();
Response.End();
}
/// after this in your page you have to add the following method
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for the
}
Thanks
saranpselvam@gmail.com
Venu510, if this helps please login to Mark As Answer. | Alert Moderator