繁体   English   中英

数据表:导出 Excel 格式

[英]Datatables: Export Excel Formatting

在使用数据表导出到 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
                }
            }
        }]
    });

}

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

// 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,例如 0.1234;
  • 0.???????????????? - 同上,但按小数点对齐

精简版,结果文件在Excel中正确打开,但快捷方式可能会影响其他XLSX文件阅读软件: JSFiddle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM