美文网首页
React下载,导入excel文件

React下载,导入excel文件

作者: 坑的心都碎了 | 来源:发表于2018-07-10 10:38 被阅读0次
    import XLSX from 'xlsx';
        importfile(e){
            const { onChange } = this.props;
            if(!e.target.files) return ;
            const file=e.target.files[0];
            var daochu = this.daochu;
            const values = this.props.form.getFieldsValue()
            const reader=new FileReader();
            reader.readAsBinaryString(file);
            reader.onload=function(e){
                const data=this.result;
                const wb=XLSX.read(data, {
                        type: 'binary'
                    })
                const params = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
    
                for(var i = 0; i<params.length; i++){
                    params[i].Name = params[i].客户名称 ;
                    delete params[i].客户名称 ;
                    params[i].GenderName = params[i].性别 ;
                    delete params[i].性别 ;
                    params[i].Tel = params[i].联系电话 ;
                    delete params[i].联系电话 ;
                    params[i].WeChat = params[i].微信 ;
                    delete params[i].微信 ;
                    // params[i].qq = params[i].qq ;
                    // delete params[i].qq ;
                    params[i].StateValue = params[i].客户状态 ;
                    delete params[i].客户状态 ;
                    params[i].SourceValue = params[i].客户来源 ;
                    delete params[i].客户来源 ;
                    params[i].Address = params[i].通信地址 ;
                    delete params[i].通信地址 ;
                    params[i].ProvinceName = params[i].省 ;
                    delete params[i].省 ;
                    params[i].CityName = params[i].市 ;
                    delete params[i].市 ;
                    params[i].CountyName = params[i].区县 ;
                    delete params[i].区县 ;
                    params[i].EducationName = params[i].学历 ;
                    delete params[i].学历 ;
                    params[i].PoliticalOutlookName = params[i].政治面貌 ;
                    delete params[i].政治面貌 ;
                    params[i].CustomerLableValue = params[i].客户标签 ;
                    delete params[i].客户标签 ;
                    params[i].Remark = params[i].备注 ;
                    delete params[i].备注 ;
                }
    
                request(`/api/C_Customer/ImportEnterpriseCustomer`, { method : 'POST' , body: params  })
                    .then((req) => {
                        if(req.Success){
                            notification.success({
                                message: '导入成功',
                                description: req.Message
                            });
                            onChange(values)
                        }else{
                            if(req.ReturnData){
                                var data = [];
                                data = req.ReturnData;
                                Modal.error({
                                    title: '导入失败',
                                    content: req.Message,
                                    okText:'下载文件',
                                    maskClosable:true,
                                    onOk: () => {
                                        daochu(data)
                                    }
                                });
                            }else{
                                Modal.error({
                                    title: '导入失败',
                                    content: '必填数据不能为空',
                                    okText:'知道了',
                                    maskClosable:true,
                                });
                            }
                        }
                    })  
            }
            this.input.value=null
        }
        daochu = (dataArr) => {
            const params = this.props.form.getFieldsValue();
            const param={
                ...params,
                StateKey:this.state.activeKey,
                SourceKey:params['SourceKey']==='null'?undefined:params['SourceKey']
            }
            const datas =  dataArr;
            const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };//这里的数据是用来定义导出的格式类型
            const wb = { SheetNames: ['Sheet1'], Sheets: {}, Props: {} };
            const data=datas.map(item=>{
                return {
                    '客户名称':item.Name,
                    '联系电话':item.Tel,
                    '客户标签':item.CustomerLableValue,
                    '客户状态':item.StateValue,
                    '距上次跟进(天)':item.OutTrackDay,
                    '客户来源':item.SourceValue,
                    '业务员':item.SalesmanName,
                    '省份':item.ProvinceName,
                    '城市':item.CityName,
                    '区县':item.CountyName,
                    '学历':item.EducationName,
                    '性别':item.Gender == 0 ? '男':'女',
                    '政治面貌':item.PoliticalOutlookName,
                    'QQ':item.QQ,
                    '类型':item.Type==1?'个人客户':'企业客户',
                    '联系次数':item.ContactNum,
                    '微信':item.WeChat,
                    '错误信息': item.ErrorInfo
                }
            })
            wb.Sheets['Sheet1'] = XLSX.utils.json_to_sheet(data);//通过json_to_sheet转成单页(Sheet)数据
            this.saveAs(new Blob([this.s2ab(XLSX.write(wb, wopts))], { type: "application/octet-stream" }), "个人客户导出" + '.' + (wopts.bookType=="biff2"?"xls":wopts.bookType));
        }
    
        // 导出excel
        saveAs(obj, fileName){
            var tmpa = document.createElement("a");
            tmpa.download = fileName || "下载";
            tmpa.href = URL.createObjectURL(obj); //绑定a标签
            tmpa.click(); //模拟点击实现下载
            setTimeout(function () { //延时释放
                URL.revokeObjectURL(obj); //用URL.revokeObjectURL()来释放这个object URL
            }, 100);
        }
    
        s2ab(s) {
            if (typeof ArrayBuffer !== 'undefined') {
                var buf = new ArrayBuffer(s.length);
                var view = new Uint8Array(buf);
                for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
                return buf;
            } else {
                var buf = new Array(s.length);
                for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
                return buf;
            }
        }
    

    相关文章

      网友评论

          本文标题:React下载,导入excel文件

          本文链接:https://www.haomeiwen.com/subject/kchfpftx.html