private void ExportToExcel(string strFileName, DataGrid dg) { HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + strFileName + ".xls"); HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.Charset = ""; HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); //dg is DataGrid records dg.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End();}
"The file you trying to open, 'filename.xls', is in a different format than specified by the file extension.Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"<br /> Yes | No | Help
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)