I have the following code:
Response.ClearContent();
Response.AddHeader( "Content-type", "application/vnd.ms-excel");
Response.AddHeader("content-disposition", "attachment;filename=test.xls");
Response.ContentType = "application/excel";
var swr = new StringWriter();
var tw = new HtmlTextWriter(swr);
grd.RenderControl(tw);
Response.Write(swr.ToString());
Response.Flush();
Response.End();
tw.Close();
swr.Close();
This action is triggered from the following bit of jquery code:
<img src="../../../../Content/images/Excel-icon.png"
onclick = "alert ($('#Filter').serialize());
$.ajax({
type: 'POST',
url: '<%=Url.Action( "Excel") %>',
data: $('#Filter').serialize(),
success : function (data, textStatus, jqXHR)
{ alert (data);},
error: function (jqXHR, textStatus, errorThrown)
{ alert (textStatus + ' ' + errorThrown);}
});" />
I have confirmed with fiddler that the headers have the expected values. I can also see the data in fiddler using the Web View.
Also, when I show the contents of data from the success function, it has the raw html for the table that I am trying to export to excel.
Thanks in advance for your help.