js-xlsx实现Excel的导入导出功能

具体代码见: 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</
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值