I have the following code in a Controller and would like to save it all to an excel file, but I can’t get the browser to show me the file save dialog.
public ContentResult Export(...) {
StringBuilder sb = new StringBuilder();
sb.Append("<table border='" + "2px" + "'b>");
//write column headings
sb.Append("<tr>");
foreach (System.Data.DataColumn dc in dt.Columns) {
sb.Append("<td><b><font face=Arial size=2>" + dc.ColumnName + "</font></b></td>");
}
sb.Append("</tr>");
//write table data
foreach (System.Data.DataRow dr in dt.Rows) {
sb.Append("<tr>");
foreach (System.Data.DataColumn dc in dt.Columns) {
sb.Append("<td><font face=Arial size=" + "14px" + ">" + dr[dc].ToString() + "</font></td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
this.Response.AddHeader("Content-Disposition", "Employees.xls");
this.Response.ContentType = "application/vnd.ms-excel";
return this.Content(sb.ToString());
}
Thanks a lot in advance!
Try this: