尝试在MVC应用中使用EPPlus下载excel表格,但未成功。它正在达到方法但没有下载。
搜索了很多方法,但没有帮助。
它是通过jQuery函数调用的,如下所示。
<script type="text/javascript">
function ExportData() {
$.ajax({
type: "GET",
url: "/Home/ExcelExport",
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
success: function (txt) {
alert('Exported');
},
failure: function (txt) {
alert('Failed');
}
});
};
</script>
控制器方法如下所示。
public void ExcelExport() {
List<BPOWorkList> exportData = new List<BPOWorkList>();
exportData = da.GetBPOWorkListData();
var data = exportData;
ExcelPackage excel = new ExcelPackage();
var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
workSheet.Cells[1, 1].LoadFromCollection(data, true);
using (var memoryStream = new MemoryStream())
{
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=ExportData.xlsx");
Response.Buffer = true;
excel.SaveAs(memoryStream);
memoryStream.Position = 0;
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
它应该在浏览器中下载文件。