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

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

    The following is a native js solution.

    function export2csv() {
      let data = "";
      const tableData = [];
      const rows = [
        ['111', '222', '333'],
        ['aaa', 'bbb', 'ccc'],
        ['AAA', 'BBB', 'CCC']
      ];
      for (const row of rows) {
        const rowData = [];
        for (const column of row) {
          rowData.push(column);
        }
        tableData.push(rowData.join(","));
      }
      data += tableData.join("\n");
      const a = document.createElement("a");
      a.href = URL.createObjectURL(new Blob([data], { type: "text/csv" }));
      a.setAttribute("download", "data.csv");
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    }

提交回复
热议问题