How to export JavaScript array info to csv (on client side)?

前端 未结 29 1667
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 21:55

I know there are lot of questions of this nature but I need to do this using JavaScript. I am using Dojo 1.8 and have all the attribute info in array, which loo

相关标签:
29条回答
  • 2020-11-21 22:33

    Create a blob with the csv data .ie var blob = new Blob([data], type:"text/csv");

    If the browser supports saving of blobs i.e if window.navigator.mSaveOrOpenBlob)===true, then save the csv data using: window.navigator.msSaveBlob(blob, 'filename.csv')

    If the browser doesn't support saving and opening of blobs, then save csv data as:

    var downloadLink = document.createElement('<a></a>');
    downloadLink.attr('href', window.URL.createObjectURL(blob));
    downloadLink.attr('download', filename);
    downloadLink.attr('target', '_blank');
    document.body.append(downloadLink);
    

    Full Code snippet:

    var filename = 'data_'+(new Date()).getTime()+'.csv';
    var charset = "utf-8";
    var blob = new Blob([data], {
         type: "text/csv;charset="+ charset + ";"
    });
    if (window.navigator.msSaveOrOpenBlob) {
         window.navigator.msSaveBlob(blob, filename);
    } else {
        var downloadLink = document.element('<a></a>');
        downloadLink.attr('href', window.URL.createObjectURL(blob));
        downloadLink.attr('download', filename);
        downloadLink.attr('target', '_blank');  
        document.body.append(downloadLink); 
        downloadLink[0].click(); 
    }
    
    0 讨论(0)
  • 2020-11-21 22:33

    The answers above work, but keep in mind that if you are opening up in the .xls format, columns ~~might~~ be separated by '\t' instead of ',', the answer https://stackoverflow.com/a/14966131/6169225 worked well for me, so long as I used .join('\t') on the arrays instead of .join(',').

    0 讨论(0)
  • 2020-11-21 22:35

    Simply try this, some of the answers here are not handling unicode data and data that has comma for example date.

    function downloadUnicodeCSV(filename, datasource) {
        var content = '', newLine = '\r\n';
        for (var _i = 0, datasource_1 = datasource; _i < datasource_1.length; _i++) {
            var line = datasource_1[_i];
            var i = 0;
            for (var _a = 0, line_1 = line; _a < line_1.length; _a++) {
                var item = line_1[_a];
                var it = item.replace(/"/g, '""');
                if (it.search(/("|,|\n)/g) >= 0) {
                    it = '"' + it + '"';
                }
                content += (i > 0 ? ',' : '') + it;
                ++i;
            }
            content += newLine;
        }
        var link = document.createElement('a');
        link.setAttribute('href', 'data:text/csv;charset=utf-8,%EF%BB%BF' + encodeURIComponent(content));
        link.setAttribute('download', filename);
        link.style.visibility = 'hidden';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    };
    
    0 讨论(0)
  • 2020-11-21 22:38

    There are two questions here:

    1. How to convert an array to csv string
    2. How to save that string to a file

    All the answers to the first question (except the one by Milimetric) here seem like an overkill. And the one by Milimetric does not cover altrenative requirements, like surrounding strings with quotes or converting arrays of objects.

    Here are my takes on this:

    For a simple csv one map() and a join() are enough:

        var test_array = [["name1", 2, 3], ["name2", 4, 5], ["name3", 6, 7], ["name4", 8, 9], ["name5", 10, 11]];
        var csv = test_array.map(function(d){
            return d.join();
        }).join('\n');
    
        /* Results in 
        name1,2,3
        name2,4,5
        name3,6,7
        name4,8,9
        name5,10,11
    

    This method also allows you to specify column separator other than a comma in the inner join. for example a tab: d.join('\t')

    On the other hand if you want to do it properly and enclose strings in quotes "", then you can use some JSON magic:

    var csv = test_array.map(function(d){
           return JSON.stringify(d);
        })
        .join('\n') 
        .replace(/(^\[)|(\]$)/mg, ''); // remove opening [ and closing ]
                                       // brackets from each line 
    
    /* would produce
    "name1",2,3
    "name2",4,5
    "name3",6,7
    "name4",8,9
    "name5",10,11
    

    if you have array of objects like :

    var data = [
      {"title": "Book title 1", "author": "Name1 Surname1"},
      {"title": "Book title 2", "author": "Name2 Surname2"},
      {"title": "Book title 3", "author": "Name3 Surname3"},
      {"title": "Book title 4", "author": "Name4 Surname4"}
    ];
    
    // use
    var csv = data.map(function(d){
            return JSON.stringify(Object.values(d));
        })
        .join('\n') 
        .replace(/(^\[)|(\]$)/mg, '');
    
    0 讨论(0)
  • 2020-11-21 22:38

    I would recommend using a library like PapaParse: https://github.com/mholt/PapaParse

    The accepted answer currently has multiple issues including:

    • it fails if the data contains a comma
    • it fails if the data contains a linebreak
    • it (kind of) fails if the data contains a quotation mark
    0 讨论(0)
  • 2020-11-21 22:38

    I use this function to convert an string[][] to a csv file. It quotes a cell, if it contains a ", a , or other whitespace (except blanks):

    /**
     * Takes an array of arrays and returns a `,` sparated csv file.
     * @param {string[][]} table
     * @returns {string}
     */
    function toCSV(table) {
        return table
            .map(function(row) {
                return row
                    .map(function(cell) {
                        // We remove blanks and check if the column contains
                        // other whitespace,`,` or `"`.
                        // In that case, we need to quote the column.
                        if (cell.replace(/ /g, '').match(/[\s,"]/)) {
                            return '"' + cell.replace(/"/g, '""') + '"';
                        }
                        return cell;
                    })
                    .join(',');
            })
            .join('\n'); // or '\r\n' for windows
    
    }
    

    Note: does not work on Internet Explorer < 11 unless map is polyfilled.

    Note: If the cells contain numbers, you can add cell=''+cell before if (cell.replace... to convert numbers to strings.

    Or you can write it in one line using ES6:

    t.map(r=>r.map(c=>c.replace(/ /g, '').match(/[\s,"]/)?'"'+c.replace(/"/g,'""')+'"':c).join(',')).join('\n')
    
    0 讨论(0)
提交回复
热议问题