Umair Malik
Umair Malik

Reputation: 1451

Exclude column from export in jQuery Datatables

I'm using jQuery datatable 1.10.11 and it's export button functionality as described here:

I want to skip last column from being export into excel file as this column has edit/delete buttons in it. My columns are generated dynamically so I can't use following method:

    $('#reservation').DataTable({
    dom: 'Bfrtip',
    buttons: [
        {
            extend: 'excel',
            text: 'Export Search Results',
            className: 'btn btn-default',
            exportOptions: {
                columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
            }
        }
    ]
});

I know this question is asked multiple time but non of them worked for me, might be version issue.

Upvotes: 28

Views: 43324

Answers (6)

Maiah Dev's
Maiah Dev's

Reputation: 71

You can try this code, I copied it from PDF button. E.g columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]. Check this documentation: https://datatables.net/extensions/buttons/examples/html5/columns.html

buttons: [
        {
            extend: 'excelHtml5',
            exportOptions: {
              columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
            }
        },
        {
            extend: 'pdfHtml5',
            exportOptions: {
              columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8]
            }
        },
        'colvis'
    ]

Upvotes: 2

Felipe Castilho
Felipe Castilho

Reputation: 501

You can add a class:

<th class='notexport'>yourColumn</th>

then exclude by class:

$('#reservation').DataTable({
   dom: 'Bfrtip',
   buttons: [
   {
        extend: 'excel',
        text: 'Export Search Results',
        className: 'btn btn-default',
        exportOptions: {
            columns: ':not(.notexport)'
        }
    }]
});

Upvotes: 50

Tushar Saha
Tushar Saha

Reputation: 887

for Excel, csv, and pdf

dom: 'lBfrtip',
buttons: [
    {
        extend: 'excelHtml5',
        text: '<i class="fa fa-file-excel-o"></i> Excel',
        titleAttr: 'Export to Excel',
        title: 'Insurance Companies',
        exportOptions: {
            columns: ':not(:last-child)',
        }
    },
    {
        extend: 'csvHtml5',
        text: '<i class="fa fa-file-text-o"></i> CSV',
        titleAttr: 'CSV',
        title: 'Insurance Companies',
        exportOptions: {
            columns: ':not(:last-child)',
        }
    },
    {
        extend: 'pdfHtml5',
        text: '<i class="fa fa-file-pdf-o"></i> PDF',
        titleAttr: 'PDF',
        title: 'Insurance Companies',
        exportOptions: {
            columns: ':not(:last-child)',
        },
    },
]

Upvotes: 1

Gyrocode.com
Gyrocode.com

Reputation: 58870

Try using CSS selector that excludes last column for columns option.

$('#reservation').DataTable({
   dom: 'Bfrtip',
   buttons: [
      {
         extend: 'excel',
         text: 'Export Search Results',
         className: 'btn btn-default',
         exportOptions: {
            columns: 'th:not(:last-child)'
         }
      }
   ]
});

Upvotes: 49

knsheely
knsheely

Reputation: 623

I just thought I'd add this in because the accepted answer only works to exclude if you are not already including something else (such as visible columns).

In order to include only visible columns except for the last column, so that you can use this in conjunction with the Column Visibility Button, use

$('#reservation').DataTable({
    dom: 'Bfrtip',
    buttons: [
    {
        extend: 'excel',
        text: 'Export Search Results',
        className: 'btn btn-default',
        exportOptions: {
            columns: ':visible:not(:last-child)'
        }
    }]
});

And if you want to explicitly add your own class:

    $('#reservation').DataTable({
    dom: 'Bfrtip',
    buttons: [
    {
        extend: 'excel',
        text: 'Export Search Results',
        className: 'btn btn-default',
        exportOptions: {
            columns: ':visible:not(.notexport)'
        }
    }]
});    

Upvotes: 12

prince sharma
prince sharma

Reputation: 9

Javascript Part:

$(document).ready(function() {
$('#example').DataTable( {
    dom: 'Bfrtip',
    buttons: [
        {
            extend: 'print',
            exportOptions: {
                // columns: ':visible' or
                columns: 'th:not(:last-child)'
            }
        },
        'colvis'
    ],
    columnDefs: [ {
        targets: -1,
        visible: false
    } ]
} );

} );

And the js files to be included:

https://code.jquery.com/jquery-3.3.1.js
https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js
https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js
https://cdn.datatables.net/buttons/1.5.2/js/buttons.print.min.js
https://cdn.datatables.net/buttons/1.5.2/js/buttons.colVis.min.js

Hope this was helpful for you. Thanks.

Upvotes: -1

Related Questions