Search the Community
Showing results for tags 'Export to excel'.
-
I found some problem with below code when i am trying to export to excel using below code on IE and Mozilla Firefox. But sometime it works fine but sometime it works fine. So can you suggest me how i can resolve the problem? public static void ToExcel(GridView _grid, string FileName) { try { _grid.AllowPaging = false; PrepareGridViewForExport(_grid); string attachment = "attachment; filename=" + FileName + ""; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.BufferOutput = true; HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", attachment); //HttpContext.Current.Response.ContentType = "application/ms-excel";//Excel 2003 //application/vnd.ms-excel HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";//Excel 2007 StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); // Create a form to contain the grid htw.WriteLine(Heading); if (!string.IsNullOrEmpty(Company)) { if (!Company.Contains("Select")) htw.WriteLine("<br/><b>Company : </b>" + Company); } if (!string.IsNullOrEmpty(Department)) { if (!Department.Contains("Select")) htw.WriteLine("<br/><b>Department : </b>" + Department); } if (!string.IsNullOrEmpty(Region)) { if (!Region.Contains("Select")) htw.WriteLine("<br/><b>Region : </b>" + Region); } htw.WriteLine("<br/><b>" + Heading + "</b> From <b>" + FromDate + "</b> To <b>" + ToDate + "</b> "); htw.WriteLine("<br/><br/>"); HtmlForm frm = new HtmlForm(); _grid.Parent.Controls.Add(frm); frm.Attributes["runat"] = "server"; frm.Controls.Add(_grid); frm.RenderControl(htw); HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html; charset=utf-8>" + Environment.NewLine); HttpContext.Current.Response.Write("HRMS Reports Sheet" + Environment.NewLine); HttpContext.Current.Response.Write(AddExcelStyling()); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.Write("</body>"); HttpContext.Current.Response.Write("</html>"); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } catch (Exception ex) { string str = ex.Message; } }