<!DOCTYPE html>
<html>
<head>
<title>js读取Excel文件</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body class="Body noover">
<input type="file" id="file"/>
<div id="demo"></div>
</body>
<script src="/common/js/util/jquery-1.9.1.min.js"></script>
<script src="https://blog-static.cnblogs.com/files/xieyongbin/xlsx.full.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

/*
FileReader共有4种读取方法:
1.readAsArrayBuffer(file):将文件读取为ArrayBuffer。
2.readAsBinaryString(file):将文件读取为二进制字符串
3.readAsDataURL(file):将文件读取为Data URL
4.readAsText(file, [encoding]):将文件读取为文本,encoding缺省值为\'UTF-8\'
*/
var wb;//读取完成的数据
var rABS = false; //是否将文件读取为二进制字符串


function fixdata(data) { //文件流转BinaryString
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;
}


$("#file").change(function(){
if(!this.files) {
return;
}
var f = this.files[0];
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
if(rABS) {
wb = XLSX.read(btoa(fixdata(data)),{
type: \'base64\'
});
} else {
wb = XLSX.read(data,{
type: \'binary\'
});
}
document.getElementById("demo").innerHTML= JSON.stringify( XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]) );
};
if(rABS) {
reader.readAsArrayBuffer(f);
} else {
reader.readAsBinaryString(f);
}
})

})
</script>
</html>

版权声明:本文为xieyongbin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xieyongbin/p/11654147.html