datatables插件导出excel【指定excel单元格格式,禁止用科学计数法显示

来源:互联网 发布:gre单词软件 编辑:程序博客网 时间:2024/06/04 18:29

1.导出excel时将单元格数据转换成文本 
datatables插件下载excel时,单元格数据是超过十位数的数字字符串(数据类型是string),下载时却用科学计数法显示,不符合需求。(最常见的就是下载身份证号码的情况)。

如果不做任何额外设置,下载的excel会如图以科学计数法的形式显示,需双击后才会显示正常。 
以科学计数法显示 
双击后显示正常: 
双击后显示正常

解决方法: 
stackoverflow上第四个答案

var table = $('#table_id').DataTable({    dom : 'Bfrtlip',        columnDefs: [{       targets:[1],//指定哪几列       render: function(data){               return "\u200C" + data ;          }    }]});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.导出excel时,更改表格中数据 
datatables官网上的例子

$(document).ready(function() {    var buttonCommon = {        exportOptions: {            format: {                body: function ( data, row, column, node ) {                    // Strip $ from salary column to make it numeric                    return column === 5 ?                        data.replace( /[$,]/g, '' ) :                        data;                }            }        }    };    $('#example').DataTable( {        ajax: '../../../../examples/ajax/data/objects.txt',        columns: [            { data: 'name' },            { data: 'position' },            { data: 'office' },            { data: 'extn' },            { data: 'start_date' },            { data: 'salary' }        ],        dom: '<"#btn_id"B>frtip',//可在此设置下载的按钮的id和样式等        buttons: [            $.extend( true, {}, buttonCommon, {                extend: 'copyHtml5'            } ),            $.extend( true, {}, buttonCommon, {                extend: 'excelHtml5'            } ),            $.extend( true, {}, buttonCommon, {                extend: 'pdfHtml5'            } )        ]    } );} );
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

阅读全文
0 0
原创粉丝点击