export html table to csv

后端 未结 8 2001
走了就别回头了
走了就别回头了 2020-12-07 18:20

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

相关标签:
8条回答
  • 2020-12-07 19:21

    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.

    0 讨论(0)
  • 2020-12-07 19:23

    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.

    0 讨论(0)
提交回复
热议问题