I am trying to add a feature of csv download option in my website. It should convert the html table present in the website in to csv content and make it downloadable. Ive be
I used Calumah's function posted above, but I did run into an issue with his code as poisted.
The rows are joined with a semicolon
csv.push(row.join(';'));
but the link generated has "text/csv" as the content type
Maybe in Windows that isn't a problem, but in Excel for Mac that throws things off. I changed the array join to a comma and it worked perfect.
I found there is a library for this. See example here:
https://editor.datatables.net/examples/extensions/exportButtons.html
In addition to the above code, the following Javascript library files are loaded for use in this example:
In HTML, include following scripts:
jquery.dataTables.min.js
dataTables.editor.min.js
dataTables.select.min.js
dataTables.buttons.min.js
jszip.min.js
pdfmake.min.js
vfs_fonts.js
buttons.html5.min.js
buttons.print.min.js
Enable buttons by adding scripts like:
<script>
$(document).ready( function () {
$('#table-arrays').DataTable({
dom: '<"top"Blf>rt<"bottom"ip>',
buttons: ['copy', 'excel', 'csv', 'pdf', 'print'],
select: true,
});
} );
</script>
For some reason, the excel export results in corrupted file, but can be repaired. Alternatively, disable excel and use csv export.