请求主要函数:
function fetchab(url, cb) { var xhr = new xmlhttprequest; xhr.open('get', url, true); xhr.responsetype = 'arraybuffer'; xhr.onload = cb; xhr.send(); };
结合js-xlsx读取xlsx文件在线示例:
<!doctype html><html><head> <meta charset="utf-8"> <title></title> <script src="http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js"></script> <script> function fetchab(url, cb) { var xhr = new xmlhttprequest; xhr.open('get', url, true); xhr.responsetype = 'arraybuffer'; xhr.onload = cb; xhr.send(); }; </script> <script> var wb; var url = function importf() {//导入 url = document.getelementbyid(url).value; if (url) { fetchab(url, function () { if (this.status == 200) { wb = xlsx.read(btoa(fixdata(this.response)), {//手动转化 type: 'base64' }); document.getelementbyid(demo).innerhtml = json.stringify(xlsx.utils.sheet_to_json(wb.sheets[wb.sheetnames[0]])); } }); } } function fixdata(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; } </script></head><body> <input type="text" id="url" /> <input type="button" onclick="importf()" value="读取" /> <div id="demo"></div></body></html>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
相关阅读:
js的随机字符串正则方法([a~z],[a~z],[0~9])
用js做的点击复制效果
浏览器html代码快速预览小工具
以上就是js-xlsx读取xlsx文件的异步详解的详细内容。
