Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript or Flash export to CSV/Excel

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 the value into the clipboard but I'd like to open directly the generated value into Excel from the browser (FF, Chrome, IE, etc...).

Thx.

like image 462
Marc Polizzi Avatar asked Nov 16 '11 10:11

Marc Polizzi


2 Answers

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.

like image 107
Ramandeep Singh Avatar answered Oct 13 '22 02:10

Ramandeep Singh


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

like image 35
LouD Avatar answered Oct 13 '22 01:10

LouD