我有一个 MVC 操作方法,它调用 ajax 将数据导出到 excel,但是打开/保存对话框没有出现。这是javascript代码:
exportExcel = function(el, e, oSettings) {
Json = self.searchCriteria;
$.post(url, Json, function(data) {
}, 'json');
};
下面是动作方法:
public ActionResult ExportToExcel(JsonDictionary args)
{
var data= GetData();
using (var package = new ExcelPackage())
{
string filename = "Data + ".xlsx";
//Create the worksheet
ExcelWorksheet ws = package.Workbook.Worksheets.Add("Data");
//Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
ws.Cells["A1"].LoadFromCollection(data, true);
var stream = new MemoryStream();
package.SaveAs(stream);
const string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
stream.Position = 0;
return File(stream, contentType, filename);
}
}