2
votes

I have two questions regarding the jQuery DataTable Excel exporting described here: https://datatables.net/extensions/buttons/examples/initialisation/export.html

First, I have a dollar amount column in which negative numbers are enclosed by parenthesis. For example, -$5.00 displays as ($5.00). Is it possible when exporting to make Excel highlight the negative dollar amount values red without highlighting them in the table?

Another less pressing question that I'll ask while I'm here regards excluding certain columns from the export. As you can see below, I'm only exporting the first 8 columns and excluding all following columns. Is there a way to exclude columns based off of an associated class on a column or something similar without having to explicitly list the column numbers I want? Thanks.

$('#tblDetail').DataTable({
    dom: 'Bfrtip',
    buttons: [
    {
        extend: 'excel',
        text: 'Export to Excel',
        exportOptions: {
            columns: [0, 1, 2, 3, 4, 5, 6, 7]
        }
    }]
});
2

2 Answers

0
votes

Another less pressing question that I'll ask while I'm here regards excluding certain columns from the export. As you can see below, I'm only exporting the first 8 columns and excluding all following columns. Is there a way to exclude columns based off of an associated class on a column or something similar without having to explicitly list the column numbers I want? Thanks.

You can add a class:

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

them exclude by class:

$('#tblDetail').DataTable({
    dom: 'Bfrtip',
    buttons: [
    {
        extend: 'excel',
        text: 'Export to Excel',
        exportOptions: {
            columns: ':not(.notexport)'
        }
    }]
});
-1
votes

Regarding the selection of columns, you can use a column-selector which includes a "jquery selector" where you can use classes, ids, etc.

About the excel formatting, the excel buttons only includes raw data. Any other data, including formats, can't be transferred to excel As per the docs, though, you could try using the sheetJS library.