mary jean ligas

mary jean ligas

  • NA
  • 420
  • 196.3k

Export to excell file

Jun 10 2014 1:13 AM
i want to create a function that would export an excell file according to a dynamically created table in web page..as in same format..please help me
 
please see my snippet:
 
GridView1.AllowPaging = true;
StringWriter tw = new StringWriter();
System.Web.UI.
HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
HtmlForm frm = new HtmlForm();
string strTmpTime = (System.DateTime.Today).ToString();
if (strTmpTime.IndexOf("/") != -1)
{
   strTmpTime = strTmpTime.Replace(
"/", "-").ToString().Trim();
}
if (strTmpTime.IndexOf(":") != -1)
{
   strTmpTime = strTmpTime.Replace(
":", "-").ToString().Trim();
}
Response.ContentType = "application/ms-excel";
Response.AddHeader(
"content-disposition", "attachment;filename=sheet.xls");
Response.Charset =
"UTF-8";
EnableViewState =
false;
Controls.Add(frm);
frm.Controls.Add(GridView1);
frm.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
 
 
this code gives an error upon opening the excel file:
 

"The file you are trying to open, [filename], is in a different format than specified by the file extension" error opening Excel files

 
 
 

Answers (4)