Girish Dubey 1 Posted August 12, 2016 Report Share Posted August 12, 2016 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; } } Quote Link to post Share on other sites
justsomeguy 1,135 Posted August 12, 2016 Report Share Posted August 12, 2016 What exactly happens when it doesn't work? Quote Link to post Share on other sites
Girish Dubey 1 Posted August 13, 2016 Author Report Share Posted August 13, 2016 when i am trying to export data in Mozilla Firefox i get a file but when i trying to open that excel file not open and display file format or extension is not correct. Quote Link to post Share on other sites
justsomeguy 1,135 Posted August 15, 2016 Report Share Posted August 15, 2016 When that happens you should save the broken file to your computer, and then open it with a text editor to check what's in it. Quote Link to post Share on other sites
Girish Dubey 1 Posted August 16, 2016 Author Report Share Posted August 16, 2016 I compared and found both file's content were same but file extension were not same. When i downloaded from google chrome file extension was ".xls" but when i was downloaded from Mozilla file extension was ".xlsx". I param i passed a grid and file name with "Report Data.xls". From google chrome file name come with "Report Data.xls" but when Download from Mozilla file name come with "Report Data.xls.xlsx". Quote Link to post Share on other sites
justsomeguy 1,135 Posted August 16, 2016 Report Share Posted August 16, 2016 You should specify the filename in one of the headers that you send, along with the content type. I believe you can use content-disposition to specify a filename. Quote Link to post Share on other sites
Girish Dubey 1 Posted August 23, 2016 Author Report Share Posted August 23, 2016 can you suggest me how i can send file name in header? Quote Link to post Share on other sites
justsomeguy 1,135 Posted August 23, 2016 Report Share Posted August 23, 2016 Like I said, you can put the filename in a content-disposition header. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.