SheetJS读取服务器Excel文件

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;
}
posted @ 2023-01-06 11:16  vvull  阅读(192)  评论(0编辑  收藏  举报