8 posts in this topic

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;
}
}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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".

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Like I said, you can put the filename in a content-disposition header.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now