【问题标题】:Datatables: Export Excel Formatting数据表:导出 Excel 格式
【发布时间】:2017-07-17 14:24:12
【问题描述】:

我在使用数据表导出到 Excel 时尝试格式化数据时遇到了一些问题。我的一列包含小数点,当在浏览器中以表格形式查看时显示 OK。当我将表格导出到 excel 时,这是对该列中的数字进行四舍五入,这是我不希望发生的。例如,在表 '220419.07109' 中显示,当导出 '220419.0711' 时,我希望这只是一个保持完整数字的字符串。

  function formatDataForExport(data, row, column, node) {

    var string = data.toString();

    return string;


}

function drawDatatable(JSONData) {

    var dataSet = [];

    table = $("#div").DataTable({
        data: dataSet,
        columns: columns(),
        columnDefs: [{
             "targets": columnTargets(showConcludedColumns),
             "visible": false,
             "searchable": false
        }],
        info: false,
        searching: false,
        paging: false,
        ordering: false,
        autoWidth: true,
        responsive: true,
        buttons: [{
            extend: 'excel',
            text: "Export to Excel",
            exportOptions: {
                columns: ":visible",
                format: {
                    body: formatDataForExport
                }
            }
        }]
    });

}

【问题讨论】:

    标签: javascript jquery excel datatables


    【解决方案1】:

    您可以使用以下解决方案。

    // Get the max value of an attribute of elements' list
    var getMaxValue = function(elements, attr) {
        var values = elements.map(function(){
            return this.getAttribute(attr) || -Infinity;
        }).toArray();
    
        return Math.max.apply(Math, values);
    }
    
    $('#example').DataTable( {
        dom: 'Bfrtip',
        columns: [
            { data: 'Number' },
        ],
        buttons: [
            {
                extend: 'excelHtml5',
                customize: function(xlsx) {
                    //Get the built-in styles
                    //refer buttons.html5.js "xl/styles.xml" for the XML structure
                    var styles = xlsx.xl['styles.xml'];
    
                    //Create a custom number format
                    //Get the available id for the custom number format
                    var numFmtId = getMaxValue($('numFmts numFmt', styles), 'numFmtId') + 1
                    //XML Node: Custom number format for the timestamp column
                    var nFmt = '<numFmt numFmtId="' + numFmtId + '" formatCode="0.######################"/>';
                    // Add new node
                    el = $('numFmts', styles);
                    el.append(nFmt).attr('count', parseInt(el.attr('count'))+1);
                    //Create our own style to use the custom number format above
                    //XML Node: Custom style for the timestamp column
                    var style = '<xf numFmtId="' + numFmtId + '" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>';
                    // Add new node
                    el = $('cellXfs', styles);
                    el.append(style).attr('count', parseInt(el.attr('count'))+1);
                    // Index of our new style
                    var styleIdx = $('xf', el).length - 1;
    
                    //Apply new style to the first (A) column
                    var sheet = xlsx.xl.worksheets['sheet1.xml'];
                    //Set new style default for the column (optional)
                    $('col:eq(0)', sheet).attr('style', styleIdx);
                    //Apply new style to the existing rows of the first column ('A'), skipping header row
                    $('row:gt(0) c[r^="A"]', sheet).attr('s', styleIdx);
                },
            },
        ]
    } );
    

    工作JSFiddle

    您可以在那里使用不同类型的格式:

    • 0.##################### - 小数点后将显示与“#”一样多的数字;
    • #.##################### - 与上面相同,但在 0.1234 等数字中没有 0;
    • 0.???????????????????????? - 同上,但以小数点对齐

    简化版,生成的文件在 Excel 中可以正确打开,但快捷方式可能会影响其他 XLSX 文件读取软件:JSFiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多