我正在使用js-xlsx,如果需要,这里是指向我的excel sheet的链接。我正在使用以下代码:
/* 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];
console.log(XLSX.utils.sheet_to_json(worksheet,{raw:true}));
}
oReq.send();
在控制台登录中,我得到的输出是:
(3) […]0: Object { FirstName: "Mayuresh", MiddleName: "Dinkar ", LastName: "Joshi", … }1: Object { FirstName: "Arun", MiddleName: "Vikas", LastName: "Pathak", … }2: Object { FirstName: "Narendra", MiddleName: "Damodardas", LastName: "Modi", … }length: 3<prototype>: Array []
我不确定如何访问该数据。我不知道叫它什么名字。我尝试了arr.length,但是它只返回1,应该返回3。JS文件返回的长度为3,但是我不确定它是从哪里提取的。我只需要一些访问该数组。谢谢
答案 0 :(得分:1)
看起来正在运行。试试看...
let worksheet = workbook.Sheets[first_sheet_name];
let objects = XLSX.utils.sheet_to_json(worksheet,{raw:true});
let names = objects.map(object => `${object.FirstName} ${object.LastName}`);
console.log(names);