How to hide all columns except for some and export to excel
How to hide all columns except for some and export to excel

Hey guys, I have a service which brings about 150 columns, but I need just to show to the user 5 of them.
I know that I can use something like this to hide columns, but I would like to know if there is the opposite way to hide/show.
columns: columns,
"columnDefs": [{
"targets": -1,
"data": null,
},
{
"targets": [3, 4],
"visible": false,
"searchable": false
},
dom: 'Bfrtip',
buttons: [{
text: 'Export to Excel',
extend: 'excelHtml5',
className: 'btn btn-primary',
exportOptions: {
columns: ':visible:not(:last-child)'
}
} ]
Can I somehow hide ALL 150 columns but only target 5 of them?
Also, I need to add the export to excel button which shows 150 columns, if I hide 145 of 150, will they appear
in the excel ??
THANKS.
This discussion has been closed.
Answers
https://datatables.net/reference/api/columns().visible()
You could use exportOptions this way as well: