我有List<model>
并且我将Javascript转换为JSON,并且当我单击按钮调用控制器方法并传递如下参数时:
$('#exceldownload').click(function(){
var json = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.ReportListModel,Newtonsoft.Json.Formatting.Indented));
json = JSON.stringify(json);
window.location = "@Url.Action("ReportExcel","Report")?model="+json+"";
});
和控制器代码:
public FileResult ReportExcel(string model)
{
var b = JsonConvert.DeserializeObject<List<ReportListModel>>(model);
if (b.Count == 0)
{
return File(Encoding.UTF8.GetBytes("empty"), "text/plain", "empty");
}
else
{
DataTable table = (DataTable)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(b), (typeof(DataTable)));
using (var excelPack = new ExcelPackage())
{
var ws = excelPack.Workbook.Worksheets.Add("WriteTest");
ws.Cells.LoadFromDataTable(table, true, OfficeOpenXml.Table.TableStyles.Light8);
var FileBytesArray = excelPack.GetAsByteArray();
return File(FileBytesArray, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "test.xlsx");
}
}
}
但是当我单击按钮并得到这样的信息时:
无法访问此站点,Localhost拒绝连接,ERR_CONNECTION_CLOSED
我想单击按钮时下载excel文件。
这行崩溃了:
window.location = "@Url.Action("ReportExcel","Report")?model="+json+"";
更改为
window.location = @Url.Action("ReportExcel","Report") + "?model="+json+"";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句