Girish Dubey Posted August 12, 2016 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; } } Link to comment Share on other sites More sharing options...
justsomeguy Posted August 12, 2016 Share Posted August 12, 2016 What exactly happens when it doesn't work? Link to comment Share on other sites More sharing options...
Girish Dubey Posted August 13, 2016 Author 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. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 15, 2016 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. Link to comment Share on other sites More sharing options...
Girish Dubey Posted August 16, 2016 Author 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". Link to comment Share on other sites More sharing options...
justsomeguy Posted August 16, 2016 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. Link to comment Share on other sites More sharing options...
Girish Dubey Posted August 23, 2016 Author Share Posted August 23, 2016 can you suggest me how i can send file name in header? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 23, 2016 Share Posted August 23, 2016 Like I said, you can put the filename in a content-disposition header. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now