Skip to content

How to export JavaScript array info to csv on client side

Mathias Rangel Wulff edited this page Jun 13, 2015 · 4 revisions

How to export JavaScript array info to CSV on client side?

Source: StackOverflow.com

Question

There are the attribute info in array, which looks like this:

    [["name1", "city_name1", ...]["name2", "city_name2", ...]]

Any idea how I can export this to csv on the client side?

Answer

You can use AlaSQL.js library to export data locally in CSV (and XLSX as well) file format.

Below you can see a simple working example how to export data to CSV format.

    <script src="http://alasql.org/console/alasql.min.js"></script>

    <button onclick="exportData()">Export data to CSV file</button>
    var data = [["Minsk",100000], ["Riga",200000]];

    function exportData() {
        alasql("SELECT * INTO CSV('cities.csv') FROM ?",[data]);
    }

You can try this example at jsFiddle.

Clone this wiki locally