具体代码见: https://download.csdn.net/download/weixin_39539270/10591644
一:导入功能
读取excel的多个sheet数据
<script type="text/javascript" src="js/xlsx.core.min.js"></script>
<table style="width:80%;margin-left:10%;margin-top:30px;">
<td align="right" style="width: 50%;">
<h3 align="center">导入参赛名单:</h3>
</td>
<td align="left" style="width: 50%;">
<input type="file" id="uploadfile" onchange="importf(this);"/>
</td>
</table>
<div style="width:80%;margin-left:10%;margin-top:30px;">
<div style="width:30%;height:100%;float:left;">
<table id="txtArea1" style="width:100%;" cellspacing="0" cellpadding="0"> </table>
</div>
<div style="width:30%;height:100%;margin-left:5%;margin-right:5%;float:left;">
<table id="txtArea2" style="width:100%;" cellspacing="0" cellpadding="0"> </table>
</div>
<div style="width:30%;height:100%;float:left;">
<table id="txtArea3" style="width:100%;" cellspacing="0" cellpadding="0"> </table>
</div>
</div>
js代码:
<script type="text/javascript">
// 遍历每张表读取
var persons1 = [];
var persons2 = [];
var persons3 = [];
function importf(obj) {
var wb;// 读取完成的数据
var f = obj.files[0];
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
wb = XLSX.read(data, {
type : 'binary'
}); // 以二进制流方式读取得到整份excel表格对象
// 表格的表格范围,可用于判断表头是否数量是否正确
var fromTo = '';
// wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
// wb.Sheets[Sheet名]获取第一个Sheet的数据JSON.stringify(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]));
for (var i = 0; i < wb.SheetNames.length; i++) {
if (wb.Sheets.hasOwnProperty(wb.SheetNames[i])) {
fromTo = wb.Sheets[wb.SheetNames[i]]['!ref']; // output: A1:D5
if(i==0){
persons1 = persons1.concat(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[i]]));
}else if(i==1){
persons2 = persons2.concat(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[i]]));
}else if(i==2){
persons3 = persons3.concat(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[i]]));
}
//break; //如果只取第一张表,就加上这行
}
}
//document.getElementById("txtArea1").innerHTML= JSON.stringify(persons1)+JSON.stringify(persons2)+JSON.stringify(persons3);
appendTr("txtArea1",persons1);
appendTr("txtArea2",persons2);
appendTr("txtArea3",persons3);
};
reader.readAsBinaryString(f);
}
function appendTr(id, array){
if(array.length>0){
for(var</