简体   繁体   中英

How to add rows when exporting data into excel in Datatable?

I try to export my datatable into Excel. Every time when exporting the table I want to add 3 default rows at the very top.

I saw a datatable message option but that might not help me because I want 3 rows.

Actual

Now I have this data (without any extra rows):没有任何额外的行

Expected

But I want to export data with these extra rows:有额外的行

I have this code right now. and i have tried adding header with message but that didn't work for me because i need 3 header rows.

$(document).ready(function() {
    var table = $('#example').DataTable( {
        lengthChange: false,
        buttons: [{ extend: 'pdf', className: 'btn-success' },
                  { extend: 'print', className: 'btn-success' },
                  { extend: 'excel', className: 'btn-success' } ]
        }
                } 
    );
    
    table.buttons().container()
        .appendTo( '#example_wrapper .col-md-6:eq(0)' );
} );

Assumed you use the button for Excel export .

You thought about the excelHtml5, Option named messageTop ?

I would rather add the 3 additional header rows viabuttons.exportData( [ options ] ) .

There you have an option key customizeData :

function customizeData (since Buttons 1.5.2) - Function that can be used to modify the data used for the export, after all of it has been gathered and pre-processed by the above formatting options. A single argument is passed in and no return parameter is expected (mutate the object passed in to alter the data):

Data for export. This is an object with the following properties:

  • header - Array of data for the header
  • footer - Array of data for the footer
  • body - 2D array of data for the body.

For example add the 3 rows to this header array:

var table = $('#myTable').DataTable();
 
var data = table.buttons.exportData( {
    customizeData: function(dataForExport) {
        dataForExport.header.push("Header line 1");
        dataForExport.header.push("Header line 2");
        dataForExport.header.push("Header line 3");
    }
} );
// Do something with the 'data' variable

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