我有一个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);
}
}
答案 0 :(得分:-1)
问题是您无法通过ajax调用下载文件。如果你看一下:
http://filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/
这将允许使用动态iFrame下载ajax文件。