代码部分:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>js导入excel</title>
</head>
<body>
<inputtype="file"onchange="importf(this)"/>
<pid="excelContent"></p>
<scriptsrc="xlsx.full.min.js"></script>
<scriptsrc="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<scriptsrc="https://cdn.bootcss.com/xlsx/0.11.5/xlsx.core.min.js"></script>
<script>
varwb;//读取
varrABS=false;
//开始导入
functionimportf(obj){
if(!obj.files){
return;
}
varf=obj.files[0];
varreader=newFileReader();
reader.onload=function(e){
vardata=e.target.result;
if(rABS){
wb=XLSX.read(btoa(fixdata(data)),{//手动转化
type:'base64'
});
}else{
wb=XLSX.read(data,{
type:'binary'
});
}
/**
*wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
*wb.Sheets[Sheet名]获取第一个Sheet的数据
*/
varexcelJson=XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
document.getElementById("excelContent").innerHTML=JSON.stringify(excelJson);
};
if(rABS){
reader.readAsArrayBuffer(f);
}else{
reader.readAsBinaryString(f);
}
}
//文件流转BinaryString
functionfixdata(data){
varo="",
l=0,
w=10240;
for(;l<data.byteLength/w;++l)o+=String.fromCharCode.apply(null,newUint8Array(data.slice(l*w,l*w+
w)));
o+=String.fromCharCode.apply(null,newUint8Array(data.slice(l*w)));
returno;
}
</script>
</body>
</html>
如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h56629.shtml