简体   繁体   中英

In Datatable Excel Export Zero Value is Coming wrong in PHP

I am exporting excel sheet from datatable Using plugins. In the sheet all values Are coming correctly except zero (0) values.

Here is the code for datatable.

$('#example').DataTable({
                     //"scrollY": 300,
                     //"scrollX": true
                      dom: 'Bfrtip',
                    buttons: [
                         { extend: 'excelHtml5', text: 'Get Excel' }
                    ],
                      "sScrollY" : "300",
                      "sScrollX" : true,
                      "bScrollCollapse" : true,
                      "bSort" : true,
                      "iDisplayLength": 5


                     });

I have attached the screen shot of the error. 在此处输入图片说明

Try using your plugins to set the field type to a number value.

Looks like 0 is being interpreted as string, which could explain the left alignment.

You have to make some modification in buttons.html5.min.js file. Find that word excelHtml5 in buttons.html5.min.js and in push method "number" === typeof a[c] || a[c].match && g.trim(a[c]).match(/^-?\\d+(.\\d+)?$/) && "0" !== a[c].charAt(0) replace "0" to 0
or another way just add one condition !== (a[c].charAt(0)&&a[c].length>1) it will work .Hope it will fulfil your requirement. All the best..!!

Just use the CSV button instead of the xcel button and it works fine: https://datatables.net/extensions/buttons/examples/html5/simple.html

Either

    $(document).ready(function() {
    $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            'copyHtml5',
            'excelHtml5',
            'csvHtml5',
            'pdfHtml5'
        ]
    } );
} )

or for a more personalised approach (assuming font-awesome, and applying a little CSS to make it look a little nicer)

    //initialise the main table
table = $('#report').DataTable( {
    "bFilter": true,
      "bSort": true,
      "bInfo": true,
      "scrollX": scrollX,
    lengthChange: true,
    fixedHeader: {
        header: true,
        footer: footerFixed
    },
    buttons: [ 
        {
            extend: 'copy',
            text: '<i class="fa fa-files-o"></i>',
            titleAttr: 'Copy'
        },
        {
            extend: 'csv',
            text:      '<i class="fa fa-file-excel-o"></i>',
            titleAttr: 'CSV',
            title: 'Subscribers'
        },
        {
            extend: 'colvis',
            text:      '<i class="fa fa-columns"></i>',
            titleAttr: 'Columns'
        },

    ],
} );

table.buttons().container()
    .appendTo( '#report_length' );

$('#report_length > label').css('padding-right', '10px');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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