Jquery 如果成功调用AJAX时DataTable中的记录数超过1条lac记录,如何自动下载excel文件?
我有一个dataTable,它有超过100000条记录。我正在尝试导出excel中的数据,如果记录数超过100k,我希望自动下载 在控制器操作方法中:Jquery 如果成功调用AJAX时DataTable中的记录数超过1条lac记录,如何自动下载excel文件?,jquery,ajax,asp.net-mvc,Jquery,Ajax,Asp.net Mvc,我有一个dataTable,它有超过100000条记录。我正在尝试导出excel中的数据,如果记录数超过100k,我希望自动下载 在控制器操作方法中: if (res.Count > 40000) { ExportDataToExcel(parameters); } 然后我创建了一个函数: public void ExportDataToExcel(some parameters) { DataTable tbl = new DataT
if (res.Count > 40000)
{
ExportDataToExcel(parameters);
}
然后我创建了一个函数:
public void ExportDataToExcel(some parameters)
{
DataTable tbl = new DataTable("SomeReport");
tbl = objCommon.GetGridDataTableFormat(some parameters);// this function is fetching me `datatable`
tbl.TableName = "Report";
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(tbl);
// wb.SaveAs("DiscountReport-" + DateTime.Now);
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename= DiscountReport.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
之后返回Ajax,成功:
success: function(data) {
something
}
上述函数参数部分的数据如下所示:
我只需要将这些数据导出到excel并自动下载。可以吗?或者我应该改变我的方法
编辑:
@帕拉什:我一定是在执行部分做错了什么。有点像汤匙喂。但以下是我在理解了逻辑之后如何实现您的代码:
$.ajax({
url: '/Reports/_GetDiscountGrid',
data: {
State: stat,
Zone: zon,
Amrac: amr,
Store: store,
Hierarchy: hirarcy,
DiscountType: discountType,
Itemcategorycode: itemcatCode,
Itemcode: code,
Period: period,
OfferNo: offernumber
},
datatype: "html",
type:"GET",
success: function(data) {
$('#tbldiscountDIV').empty();
if (typeof (data) == "string") {
$('#tbldiscountDIV2').show();
DownLoadSheet(data, "DiscountReport.xlsx");
}
在此之后,我将函数定义为:
function DownLoadSheet(data,fileName)
{
const a = document.createElement("a");
document.body.appendChild(a);
a.style="display:none";
return function (data,fileName)
{
const blob = new Blob([data], { type: "application/vnd.openxmlfomrats-officedocument.spreadsheetml.sheet" }),
url = window.URL.createObjectURL(blob);
a.href=url;
a.download=fileName;
a.click();
window.URL.revokeObjectURL(url);
}//return for function end
a.href = url;
var temp = blob;
}
在返回AJAX调用并退出之后。我发现target和download参数是空的,我哪里出错了
[excel格式的数据在
success
方法中,xlsx文件实际上是作为响应返回的。您可以按如下方式下载:
const DownloadSheet = (function() {
const a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function(data, fileName) {
const blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};
}());
$.ajax({
type: "GET",
url: url,
success: function(response) {
DownloadSheet(response, 'DiscountReport.xlsx')
}
});
@帕拉什:请检查编辑的部分和我添加的图像。我知道我很接近,但在实现部分的某个地方我出错了。无需检查
if(typeof(data)=“string”)
只需运行DownLoadSheet(data,“DiscountReport.xlsx”);
内部成功并再次测试。