Jump to content

Girish Dubey

Recommended Posts

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

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

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
×
×
  • Create New...