1

我有一个 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);
        }
    }
4

1 回答 1

-1

问题是您无法通过 ajax 调用下载文件。如果你看这个:

http://filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/

这将允许使用动态 iFrame 下载 ajax 文件。

于 2013-06-11T15:39:47.193 回答