I am trying to export table to excel below is my code, its working in chrome but not in IE. I am using MVC 5
{
return View(db.tbl_GR_Reports.ToList());
}
public ActionResult ExportData()
{
GridView gv = new GridView();
gv.DataSource = db.tbl_GR_Reports.ToList();
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=list.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return RedirectToAction("tbl_GR_Reports");
}
View Code:
@model IEnumerable<ExporttoExcel.Models.tbl_GR_Reports>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table style="border: 1px solid black;border-collapse:collapse;" width="900" align="center">
<tr>
<th>
@Html.DisplayNameFor(model => model.Report_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Query)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td width="10%" style="border-bottom:1px solid black; border-right:1px solid black">
@Html.DisplayFor(modelItem => item.Report_Name)
</td>
<td width="10%" style="border-bottom:1px solid black; border-right:1px solid black">
@Html.DisplayFor(modelItem => item.Description)
</td>
<td width="40%" style="border-bottom:1px solid black; border-right:1px solid black">
@Html.DisplayFor(modelItem => item.Query)
</td>
</tr>
}
</table>
@using (Html.BeginForm("ExportData", "tbl_GR_Reports", FormMethod.Post))
{
<table>
<tr><td></td><td><input type="submit" name="Export" id="Export" value="Export" /></td></tr>
</table>
}
Thanks,