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

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

    Download CSV File

      let csvContent = "data:text/csv;charset=utf-8,";
      rows.forEach(function (rowArray) {
        for (var i = 0, len = rowArray.length; i < len; i++) {
          if (typeof (rowArray[i]) == 'string')
            rowArray[i] = rowArray[i].replace(/<(?:.|\n)*?>/gm, '');
          rowArray[i] = rowArray[i].replace(/,/g, '');
        }
    
        let row = rowArray.join(",");
        csvContent += row + "\r\n"; // add carriage return
      });
      var encodedUri = encodeURI(csvContent);
      var link = document.createElement("a");
      link.setAttribute("href", encodedUri);
      link.setAttribute("download", "fileName.csv");
      document.body.appendChild(link);
      link.click();
    

提交回复
热议问题