Faqs on asp.net,c#,vb.net and sqlserver2005

this blog covers all the Faqs related to .net technologies like asp.net,vb.net,c#.net,ajax,javascript and sqlserver2005.

Mar 10, 2008

Export GridData to Excel


Export data from Datagrid to Excel

private void btnExportToExcel_Click(object sender,System.EventArgs e)
{
ExportToExcel("Report.xls",DataGrid1);
}
private void ExportToExcel(string strFileName,DataGrid dg)
{
Response.Clear();
Response.Buffer = true;Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition","attachment;filename=" + strFileName);
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter sw = new StringWriter();
System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

Happy Programming

0 Comments:

Post a Comment

<< Home