Marc Polizzi
Marc Polizzi

Reputation: 9375

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.

Upvotes: 7

Views: 8521

Answers (2)

LouD
LouD

Reputation: 3844

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.

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

Upvotes: 2

Ramandeep Singh
Ramandeep Singh

Reputation: 5253

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.

Upvotes: 9

Related Questions