Exporting datagrid to excel

Dec 3, 2008 at 4:00 AM
Edited Dec 3, 2008 at 4:07 AM
Hii..the following code i used to export datagrid to excel and it worked fine with mS office 2007....but just before the excel file opens,it shows a warning telling that the file is not a supported type and asking whether to continue..
how can i avoid this so that i get the datagrid exported to excel without any issues ,with  MS office 2003 ,XP and 2007?
am working with Visual studio 2005..
Pls do help me with this.......thanks in advance...

here is the code i used inside a button click..

Response.Clear();

tring filename2 = TextBox4.Text;

Response.AddHeader(

 

"content-disposition", "attachment;filename=" + filename2 + ".xls");

Response.Charset =

 

"";

Page.EnableViewState =

 

false;

 

Response.ContentType =

"application/vnd.xls";

System.IO.

 

StringWriter stringWriter = new System.IO.StringWriter(); 

System.Web.UI.

 

HtmlTextWriter htmwrite = new HtmlTextWriter(stringWriter);

DataGrid2.RenderControl(htmwrite);

Response.Write(stringWriter.ToString());

Response.End();

 

Dec 3, 2008 at 5:55 AM
Hi Anu787, I would recommend taking this question to the ASP.NET forums.  This forum is for discussion about the CodePlex website only.