DataTables.net 导出按钮未生成 Excel 文件
DataTables.net Export Button not generating Excel file
单击 Datatables.net 的“导出”按钮时出现以下错误。
以下是我的代码
$.ajax({
type: "POST",
url: uri,
data: JSON.stringify(args),
contentType: "application/json;charset=utf-8",
success: function (data, status, xhr) {
//alert("The result is : " + data);
if (!data.d) {
$("#gvCurr").DataTable();
}
else {
$("#gvCurr").DataTable({
"aaData": JSON.parse(data.d),
"bDestroy": true,
dom: 'Bfrtip',
deferRender: true,
"bLengthChange": false,
"bPaginate": false,
buttons: [
{
extend: 'excel',
text: '<i class="fas fa-file-excel"></i> Export',
className: "btn btn-primary",
filename: 'DomesticInvoiceReport - ' + moment().format("DD-MMM-YYYY"),
}
],
"columns": [
{ "data": "ProjectNo" },
{ "data": "CountryName" },
{ "data": "StateName" },
{ "data": "SectorName" },
{ "data": "CoOrdName" },
{ "data": "Curr1" },
{ "data": "InvoiceNo_1" },
{ "data": "InvoiceDate_1" },
{ "data": "Month_1" },
{ "data": "Year_1" },
{ "data": "TotalFee_1" },
{ "data": "EscalationAmt_1" },
{ "data": "CurrentOPEAmt_1" },
{ "data": "CGSTPerc" },
{ "data": "SGSTPerc" },
{ "data": "TotalTaxPerc_1" },
{ "data": "TotalTaxAmt_1" },
{ "data": "CurrentInvoiceAmt_1" },
{
mRender: function (data, type, row) {
if (row.IsWithheld_1 == "" || row.IsWithheld_1 == 0)
return 'No';
}
},
{ "data": "WithheldAmt_1" },
{ "data": "BalanceInHandUptoThisInv_1" }
],
"order": [[0, "asc"]]
});
}
$("#entry").hide();
$("#list").show();
},
error: function (xhr) {
alert(xhr.responseText);
}
});
如果您看到代码,如果我注释掉 {"data":"Year_1"} 之后的列,它会生成 excel 文件,如果我包括之后的列,它给我错误显示在所附的图像中。所以不是代码不正确或者js文件顺序不正确的问题
我已将这些设置包含在 web.config
<httpRuntime targetFramework="4.7.2" maxRequestLength="2147483647" requestLengthDiskThreshold="2097152" executionTimeout="240" />
<jsonSerialization maxJsonLength="2147483644" />
检索时正确显示记录。导出时出现错误。我不确定我在这里错过了什么。
我相信你有这个问题:
{
mRender: function (data, type, row) {
if (row.IsWithheld_1 == "" || row.IsWithheld_1 == 0)
return 'No';
}
},
- 渲染回调应用于列定义,它们不能单独存在
- 你必须return一个值,如果你return
undefined
你会得到“trim 不是函数 "
正确的做法是
{
data: "IsWithheld_1",
render: function(data, type, row) {
return (data === '' || data == 0) ? 'No' : ''
}
}
单击 Datatables.net 的“导出”按钮时出现以下错误。
以下是我的代码
$.ajax({
type: "POST",
url: uri,
data: JSON.stringify(args),
contentType: "application/json;charset=utf-8",
success: function (data, status, xhr) {
//alert("The result is : " + data);
if (!data.d) {
$("#gvCurr").DataTable();
}
else {
$("#gvCurr").DataTable({
"aaData": JSON.parse(data.d),
"bDestroy": true,
dom: 'Bfrtip',
deferRender: true,
"bLengthChange": false,
"bPaginate": false,
buttons: [
{
extend: 'excel',
text: '<i class="fas fa-file-excel"></i> Export',
className: "btn btn-primary",
filename: 'DomesticInvoiceReport - ' + moment().format("DD-MMM-YYYY"),
}
],
"columns": [
{ "data": "ProjectNo" },
{ "data": "CountryName" },
{ "data": "StateName" },
{ "data": "SectorName" },
{ "data": "CoOrdName" },
{ "data": "Curr1" },
{ "data": "InvoiceNo_1" },
{ "data": "InvoiceDate_1" },
{ "data": "Month_1" },
{ "data": "Year_1" },
{ "data": "TotalFee_1" },
{ "data": "EscalationAmt_1" },
{ "data": "CurrentOPEAmt_1" },
{ "data": "CGSTPerc" },
{ "data": "SGSTPerc" },
{ "data": "TotalTaxPerc_1" },
{ "data": "TotalTaxAmt_1" },
{ "data": "CurrentInvoiceAmt_1" },
{
mRender: function (data, type, row) {
if (row.IsWithheld_1 == "" || row.IsWithheld_1 == 0)
return 'No';
}
},
{ "data": "WithheldAmt_1" },
{ "data": "BalanceInHandUptoThisInv_1" }
],
"order": [[0, "asc"]]
});
}
$("#entry").hide();
$("#list").show();
},
error: function (xhr) {
alert(xhr.responseText);
}
});
如果您看到代码,如果我注释掉 {"data":"Year_1"} 之后的列,它会生成 excel 文件,如果我包括之后的列,它给我错误显示在所附的图像中。所以不是代码不正确或者js文件顺序不正确的问题
我已将这些设置包含在 web.config
<httpRuntime targetFramework="4.7.2" maxRequestLength="2147483647" requestLengthDiskThreshold="2097152" executionTimeout="240" />
<jsonSerialization maxJsonLength="2147483644" />
检索时正确显示记录。导出时出现错误。我不确定我在这里错过了什么。
我相信你有这个问题:
{
mRender: function (data, type, row) {
if (row.IsWithheld_1 == "" || row.IsWithheld_1 == 0)
return 'No';
}
},
- 渲染回调应用于列定义,它们不能单独存在
- 你必须return一个值,如果你return
undefined
你会得到“trim 不是函数 "
正确的做法是
{
data: "IsWithheld_1",
render: function(data, type, row) {
return (data === '' || data == 0) ? 'No' : ''
}
}