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

前端 未结 29 1666
没有蜡笔的小新
没有蜡笔的小新 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:30

    The solution from @Default works perfect on Chrome (thanks a lot for that!) but I had a problem with IE.

    Here's a solution (works on IE10):

    var csvContent=data; //here we load our csv data 
    var blob = new Blob([csvContent],{
        type: "text/csv;charset=utf-8;"
    });
    
    navigator.msSaveBlob(blob, "filename.csv")
    
    0 讨论(0)
  • 2020-11-21 22:30

    In case anyone needs this for knockout js, it works ok with basically the proposed solution:

    html:

    <a data-bind="attr: {download: filename, href: csvContent}">Download</a>
    

    view model:

    // for the download link
    this.filename = ko.computed(function () {
        return ko.unwrap(this.id) + '.csv';
    }, this);
    this.csvContent = ko.computed(function () {
        if (!this.csvLink) {
            var data = ko.unwrap(this.data),
                ret = 'data:text/csv;charset=utf-8,';
    
            ret += data.map(function (row) {
                return row.join(',');
            }).join('\n');
    
            return encodeURI(ret);
        }
    }, this);
    
    0 讨论(0)
  • 2020-11-21 22:31

    A lot of roll-your-own solutions here for converting data to CSV, but just about all of them will have various caveats in terms of the type of data they will correctly format without tripping up Excel or the likes.

    Why not use something proven: Papa Parse

    Papa.unparse(data[, config])
    

    Then just combine this with one of the local download solutions here eg. the one by @ArneHB looks good.

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

    Here's how I download CSV files on the client side in my Java GWT application. Special thanks to Xavier John for his solution. It's been verified to work in FF 24.6.0, IE 11.0.20, and Chrome 45.0.2454.99 (64-bit). I hope this saves someone a bit of time:

    public class ExportFile 
    {
    
        private static final String CRLF = "\r\n";
    
        public static void exportAsCsv(String filename, List<List<String>> data) 
        {
            StringBuilder sb = new StringBuilder();
            for(List<String> row : data) 
            {
                for(int i=0; i<row.size(); i++)
                {
                    if(i>0) sb.append(",");
                    sb.append(row.get(i));
                }
                sb.append(CRLF);
            }
    
            generateCsv(filename, sb.toString());
        }
    
        private static native void generateCsv(String filename, String text)
        /*-{
            var blob = new Blob([text], { type: 'text/csv;charset=utf-8;' });
    
            if (navigator.msSaveBlob) // IE 10+
            { 
                navigator.msSaveBlob(blob, filename);
            } 
            else 
            {
                var link = document.createElement("a");
                if (link.download !== undefined) // feature detection
                { 
                    // Browsers that support HTML5 download attribute
                    var url = URL.createObjectURL(blob);
                    link.setAttribute("href", url);
                    link.setAttribute("download", filename);
                    link.style.visibility = 'hidden';
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                }
            }
        }-*/;
    }
    
    0 讨论(0)
  • 2020-11-21 22:32

    People are trying to create their own csv string, which fail on edge cases, e.g. special characters, surely this is a solved problem right?

    papaparse - use for JSON to CSV encoding. Papa.unparse().

    import Papa from "papaparse";
    
    const downloadCSV = (args) => {  
    
      let filename = args.filename || 'export.csv';
      let columns = args.columns || null;
    
      let csv = Papa.unparse({ data: args.data, fields: columns})
      if (csv == null) return;
    
      var blob = new Blob([csv]);
      if (window.navigator.msSaveOrOpenBlob)  // IE hack; see http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx
          window.navigator.msSaveBlob(blob, args.filename);
      else
      {
          var a = window.document.createElement("a");
          a.href = window.URL.createObjectURL(blob, {type: "text/plain"});
          a.download = filename;
          document.body.appendChild(a);
          a.click();  // IE: "Access is denied"; see: https://connect.microsoft.com/IE/feedback/details/797361/ie-10-treats-blob-url-as-cross-origin-and-denies-access
          document.body.removeChild(a);
      }
    
    }
    

    Example usage

    downloadCSV({ 
      filename: 'filename.csv',
      data: [{'a': '1', 'b': 2'}],
      columns: ['a','b']
    });
    

    https://github.com/mholt/PapaParse/issues/175 - See this comment for browser support discussion.

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

    I came here looking for a bit more RFC 4180 compliance and I failed to find an implementation, so I made a (possibly inefficient) one for my own needs. I thought I would share it with everyone.

    var content = [['1st title', '2nd title', '3rd title', 'another title'], ['a a a', 'bb\nb', 'cc,c', 'dd"d'], ['www', 'xxx', 'yyy', 'zzz']];
    
    var finalVal = '';
    
    for (var i = 0; i < content.length; i++) {
        var value = content[i];
    
        for (var j = 0; j < value.length; j++) {
            var innerValue =  value[j]===null?'':value[j].toString();
            var result = innerValue.replace(/"/g, '""');
            if (result.search(/("|,|\n)/g) >= 0)
                result = '"' + result + '"';
            if (j > 0)
                finalVal += ',';
            finalVal += result;
        }
    
        finalVal += '\n';
    }
    
    console.log(finalVal);
    
    var download = document.getElementById('download');
    download.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(finalVal));
    download.setAttribute('download', 'test.csv');
    

    Hopefully this will help someone out in the future. This combines both the encoding of the CSV along with the ability to download the file. In my example on jsfiddle. You can download the file (assuming HTML 5 browser) or view the output in the console.

    UPDATE:

    Chrome now appears to have lost the ability to name the file. I'm not sure what's happened or how to fix it, but whenever I use this code (including the jsfiddle), the downloaded file is now named download.csv.

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