Search code examples
datatablesexport-to-excelexport-to-csv

Datatables Export to CSV and Excel File Extension Missing


I followed the following example. However, whenever I click on the CSV and Excel file export button I only get a file without its extension. It would be cumbersome for my final users to manually add the file extension so I was wondering if there is something that needs to be fixed. I have already read the the source code in the example but I have not found anything different. The source code to create the table is pretty straightforward.

$('#example').DataTable( {
    dom: 'Bfrtip',
    buttons: [
        'copy', 'csv', 'excel', 'pdf', 'print'
    ]
} );

Thank you very much for your help.


Solution

  • I found out that I needed to add the title and extension options to the table code. If any of those is missing I would get a file without the appropriate extension.

    $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            'copy', 
            {
                extend: 'csv',
                text: 'csv',
                extension: '.csv',
                exportOptions: {
                    modifier: {
                        page: 'current'
                    }
                },
                title: 'table'
            }, 
            'pdf', 
            'print',
            {
                extend: 'excel',
                text: 'excel',
                extension: '.xlsx',
                exportOptions: {
                    modifier: {
                        page: 'current'
                    }
                },
                title: 'table'
            }
        ]
    } );