我需要从本地XLSX文件读取数据并将其保存到对象(或数组)。
数据如下:
我需要这样的数据:
所以我可以使用它(将它与其他对象进行比较/显示...)
这怎么可能?
I tried an AJAX call with the following code:
/* set up XMLHttpRequest */
var url = "Test.xlsx";
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(e) {
var arraybuffer = oReq.response;
/* convert data to binary string */
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for(var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
var bstr = arr.join("");
/* Call XLSX */
var workbook = XLSX.read(bstr, {type:"binary"});
/* DO SOMETHING WITH workbook HERE */
var first_sheet_name = workbook.SheetNames[0];
/* Get worksheet */
var worksheet = workbook.Sheets[first_sheet_name];
var test = XLSX.utils.sheet_to_json(worksheet,{raw:true})
console.log(test)
}
oReq.send();
console.log(test) //I can not access it here..
它工作了,我按需获得了对象,但问题是我无法在函数外部访问该对象。 我几个小时前发了一条帖子: How to get an object from an async call (xlsx file) to global scope