I am trying to export a GridView
to Excel.
I have tried to follow steps found here:
- http://www.programming-free.com/2012/09/aspnet-export-grid-view-to-excel.html#.UhUREpK1F9o
- export gridview to excel file
And other similar sites.
My GridView
does not have any special properties different from default
and my SqlDataSource
uses filterExpression
if that is important.
When I try the above mentioned solutions no exception occurs, but the excel is not produced.
UPDATE
I forgot to mention that the GridView
is inside a asp:Content
control. I heard
this might matter.
My code-behind goes something like this ( I have tried multiple things ).
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = String.Empty;
EnableViewState = false;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView3.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
HttpContext.Current.ApplicationInstance.CompleteRequest();