Javascript or Flash export to CSV/Excel

后端 未结 2 1514
我在风中等你
我在风中等你 2021-01-05 05:57

Is there anyway to export JSON data to CSV/Excel without any interaction with the server side? Using Javascript only? or Flash? I\'m currently using ZeroClipboard to copy th

相关标签:
2条回答
  • 2021-01-05 06:26

    Far and away, the cleanest, easiest export from tables to Excel is Jquery DataTables Table Tools plugin. You get a grid that sorts, filters, orders, and pages your data, and with just a few extra lines of code and two small files included, you get export to Excel, PDF, CSV, to clipboard and to the printer.

    This is all the code that's required:

    $(document).ready( function () {
        $('#example').dataTable( {
            "sDom": 'T<"clear">lfrtip',
            "oTableTools": {
            "sSwfPath": "/swf/copy_cvs_xls_pdf.swf"
            }
        } );
    } );
    

    So, quick to deploy, no browser limitations, no server-side language required, and most of all very EASY to understand. It's a win-win. The one thing it does have limits on, though, is strict formatting of columns.

    0 讨论(0)
  • 2021-01-05 06:34

    There doesn't seem to be a foolproof way to do it client-side only on all browsers and file sizes. All solutions seem to use one of the following:

    • Flash plugin (ex: Downloadify): bugs and not mobile compatible
    • Data URL: limited IE support and size restrictions
    • HTML5: Download methods not standardized across browsers.

    Depending on your use case you might be able to get away with one of the above though. Here are some other posts on SO with more details.

    • Export HTML table to csv file on client side
    • Export to csv in jQuery

    Note that this part of the answer from Ramandeep Singh is incorrect:

    So, quick to deploy, no browser limitations, no server-side language required, and most of all very EASY to understand.

    DataTables does use a flash plugin if you look at the code. It will not work on mobile browsers (iOS, recent Android devices without rooting, Windows 8 RT without hacks or MSFT approval process). Here is a post from their site as well: http://www.datatables.net/forums/discussion/7563/export-to-csvpdf-without-tabletools/p1

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