[英]How to use XLSX library in Javascript to parse an Excel file from a particular row
I am currently using the library XLSX
in Javascript to convert an Excel file into JSON object and it is giving me the results as expected.我目前正在使用 Javascript 中的库
XLSX
将 Excel 文件转换为 JSON ZA8CFDE63311BD59EB2666ZB8
However, the code I am currently using parses the entire Excel sheet and I don't want that.但是,我目前使用的代码会解析整个 Excel 工作表,我不希望这样。 I want to start parsing the file from a particular row (row number 14).
我想从特定行(第 14 行)开始解析文件。
Can this be done using the XLSX?这可以使用 XLSX 完成吗? If yes, how?
如果是,如何?
Below is my current JS code (taken from https://levelup.gitconnected.com/how-to-convert-excel-file-into-json-object-by-using-javascript-9e95532d47c5 )下面是我当前的 JS 代码(取自https://levelup.gitconnected.com/how-to-convert-excel-file-into-json-object-by-using-javascript-9e95532d47c5 )
var selectedFile;
document.getElementById("fileUpload")
.addEventListener("change", function (event) {
selectedFile = event.target.files[0]
});
document.getElementById("upload")
.addEventListener("click", function () {
if (selectedFile) {
var fileReader = new FileReader();
fileReader.onload = function (event) {
var data = event.target.result;
var workbook = XLSX.read(data, {
type: "binary"
});
workbook.SheetNames.forEach(sheet => {
let rowObject = XLSX.utils.sheet_to_row_object_array(
workbook.Sheets[sheet]
);
let jsonObject = JSON.stringify(rowObject, null, '\t');
document.getElementById("jsonData").innerHTML = jsonObject;
});
};
fileReader.readAsBinaryString(selectedFile);
}
});
I am answering my own question so that it might be helpful for anyone later.我正在回答我自己的问题,以便以后对任何人都有帮助。
let rowObject = XLSX.utils.sheet_to_row_object_array
(
workbook.Sheets[sheet], {range: 14}
);
The parameter range can be added in the given function which is a part of the code.参数范围可以添加到代码的一部分给定的 function 中。
The Excel parses from line 14 now and not the beginning of the page. Excel 现在从第 14 行解析,而不是页面的开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.