【问题标题】:DataTables.net Export Button not generating Excel fileDataTables.net 导出按钮不生成 Excel 文件
【发布时间】:2020-10-04 12:38:49
【问题描述】:

单击 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);
    }
});

如果你看到 int 代码,如果我注释掉 { "data": "Year_1" } 之后的列,它正在生成 excel 文件,如果我在之后包含列,它会给我显示的错误附上的图片。所以不是代码不正确或者js文件顺序不正确的问题

我已将这些设置包含在 web.config 中

<httpRuntime targetFramework="4.7.2" maxRequestLength="2147483647" requestLengthDiskThreshold="2097152" executionTimeout="240" />    

<jsonSerialization maxJsonLength="2147483644" />

它在检索时正确显示记录。在导出时它给出错误。我不确定我在这里缺少什么。

【问题讨论】:

    标签: c# jquery datatables-1.10


    【解决方案1】:

    我相信你有这个问题:

    {
      mRender: function (data, type, row) {
         if (row.IsWithheld_1 == "" || row.IsWithheld_1 == 0)
             return 'No';
         }
    },
    
    1. 渲染回调适用于列定义,它们不能单独存在
    2. 必须返回一个值,如果你返回undefined你会得到“trim is not a function

    正确的做法是

    { 
      data: "IsWithheld_1",
      render: function(data, type, row) {
        return (data === '' || data == 0) ? 'No' : ''
      }
    }
    

    【讨论】:

    • 是的。这就是问题所在。我改变了它,它起作用了。
    猜你喜欢
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    相关资源
    最近更新 更多