简体   繁体   中英

How can I separate data to new sheet in same excel file

If I have data within array2D and I want to separate each Device to each sheet in excel file. From my code below here where's the line do I have to change about exportToCSV() to solve this problem.

const rows = [
  ["DeviceA"]
  ["Date/Time", "smokeSensor", "fireSensor"],
  ["190501 00:01", "200", "700"],
  ["190501 00:02", "300", "750"],
  ["190502 00:01", "20", "780"],
  ["190502 00:02", "30", "630"],
  [""],
  ["DeviceB"],
  ["Date/Time", "smokeSensor", "fireSensor"],
  ["190501 00:01", "100", "600"],
  ["190501 00:02", "110", "522"],
  ["190502 00:01", "120", ""],
  ["190502 00:02", "130", ""],
];

function exportToCSV(rows) {
      let csvContent = ""
      rows.forEach(function(rowArray) {
          let row = rowArray.join(",")
          csvContent += row + "\r\n"
      });
      var link = document.createElement("a")
      document.body.appendChild(link)
      // var blob = new Blob([csvContent], {type: "text/csv"}); 
      var blob = new Blob([csvContent], {type: "xls/xlsx"})
      var url = window.URL.createObjectURL(blob)
      link.setAttribute("href", url)
      link.setAttribute("download", "my_data.csv")
      link.click() /* Download the data file named "my_data.csv". */
}

CSV does not support the separation of the data in sheets.

I created a JavaScript library called ExcellentExport to export tables or arrays to CSV or Excel.

The excel export has support for sheets:

ExcellentExport.convert({ anchor: this, filename: 'data_123.array', format: 'xlsx'}, [
    {name: 'Sheet Name Here 1', from: {array: yourArray1}},
    {name: 'Sheet 2', from: {array: yourArray2}},
]);

For CSV parsing you can use Papa Parse. You can parse CSV to JSON and do the things and again you can parse back from JSON to CSV

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM