Adding meta information above the table when exporting to Excel/CSV?

Adding meta information above the table when exporting to Excel/CSV?

BrawlBrawl Posts: 2Questions: 1Answers: 0
edited July 2022 in Free community support

Hello,

I have followed the guide here:

https://datatables.net/extensions/buttons/examples/initialisation/export.html

And I now have a table rendered with Excel/CSV buttons which work fine.

However, when exporting, I want to add a row above the table with some metadata e.g. the query they used to search my database (call it 'textQuery')

My JS code within my home.html file is currently:

<script>
    $(document).ready(function () {
        $('#table_id').DataTable({
            dom: 'Brtip',
            buttons: [
            'copy', 'csv', 'excel']
           });
       });  
</script>

I have looked at exportOptions, but I cannot see anything which does what I need, although I admit I am VERY new to this.

Any help would be much appreciated! :smile:

Answers

  • BrawlBrawl Posts: 2Questions: 1Answers: 0
    edited July 2022

    As always, as soon as one asks a question, one finds an answer. Thank you anyway:

           $(document).ready(function () {
                $('#table_id').DataTable({
                    dom: 'Brtip',
                            {
                extend: 'excel',
          customize: function ( xlsx ) {
            var sheet = xlsx.xl.worksheets['sheet1.xml'];
            $('c[r=A1] t', sheet).text( 'Custom Heading in First Row' );
            $('row:first c', sheet).attr( 's', '2' ); // first row is bold
          }
        }                });
            });  
    
  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    You could do this for example:

    <script>
        $(document).ready(function () {
            $('#table_id').DataTable({
                dom: 'Brtip',
                buttons: [
                'copy', 'csv', 
                { extend: "excel",
                   title:    function () { return "yourTitle" },
                   messageBottom: function () { return "yourMessageAtTheBottom" }
                 }]
               });
           }); 
    </script>
    

    Here is another example also including messageTop.
    https://datatables.net/extensions/buttons/examples/html5/titleMessage.html

Sign In or Register to comment.