https://docs.sheetjs.com/docs/demos/network#xmlhttprequest
var filename = 'Test.xlsx';
var req = new XMLHttpRequest();
req.open('GET', filename, true);
req.responseType = 'arraybuffer';
req.onload = function (e) {
//兼容IE,需把type改为binary,并对req.response进行转化
var workbook = XLSX.read(arrayBufferToBinaryString(req.response), {
type: 'binary'
});
console.log(workbook);
}
req.send();
//ArrayBuffer转BinaryString转BinaryString
function arrayBufferToBinaryString(data) {
var o = '',
l = 0,
w = 10240;
for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
return o;
}
该代码示例展示了如何利用XMLHttpRequest的GET请求获取Excel文件(Test.xlsx),并将响应类型设置为arraybuffer。然后,通过SheetJS库将接收到的数据转换为二进制字符串,从而读取和解析Excel工作簿内容。
2912

被折叠的 条评论
为什么被折叠?



