[英]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.