简体   繁体   English

如何在同一个excel文件中将数据分隔到新工作表

[英]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. 如果我在array2D中有数据,我想将每个设备分开到excel文件中的每个工作表。 From my code below here where's the line do I have to change about exportToCSV() to solve this problem. 从我下面的代码这里我需要更改exportToCSV()来解决这个问题。

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. CSV不支持在工作表中分离数据。

I created a JavaScript library called ExcellentExport to export tables or arrays to CSV or Excel. 我创建了一个名为ExcellentExport的JavaScript库,用于将表或数组导出为CSV或Excel。

The excel export has support for sheets: excel导出支持工作表:

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. 对于CSV解析,您可以使用Papa Parse。 You can parse CSV to JSON and do the things and again you can parse back from JSON to CSV 您可以将CSV解析为JSON并执行操作,您可以再次从JSON解析为CSV

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不删除先前数据的情况下,在同一文件中写入新的Excel工作表(nodeJS) - Writing to a new excel sheet in the same file without deleting previous data (nodeJS) 如何使用地图将一列分隔到他们自己的工作表中? - How can I separate a column into their own sheet using maps? 如何用节点 js 中的新数据覆盖我的 xlsx 工作表数据 - How can I overwrite my xlsx sheet data with new data in node js 如何从Excel工作表或.csv文件向jsfiddle下拉列表输入数据? - How to input data to jsfiddle dropdown from an Excel Sheet or .csv file? 如何将相同范围从文件夹中的每个文件合并到母版纸上? - How can I merge Same Range from each file in a folder onto a master sheet? 如何在不创建新的单独文件的情况下在页面链接上添加密码保护? - How can I add password protection on a link to a page without creating a new separate file? 如何在同一 HTML 页面上显示 HTML GET 请求数据,使用单独的 PHP 文件来获取它? - How do I display the HTML GET request data on the same HTML page, using a separate PHP file to fetch it? 尝试创建一个单独的文件,我可以在包含“new”的 JavaScript 中导入 - Trying to create a separate file I can import in JavaScript with "new" involved 只有在我想要的工作表上输入新数据时,如何才能在 Google Apps 脚本中运行 onChange 或 onEdit? - How can I run onChange or onEdit in Google Apps Script only when new data is entered on the sheet I want? 将一张工作表中的原始数据排序到Google App脚本中新工作表中的单独标签中 - Sorting raw data from one sheet into separate tabs on a new sheet in Google App Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM