.net core+Vue Excel文件导入

Vue2.0 文件上传组件代码,弹窗代码(Element.vue)饿了么的ui组件


            
                
                
{{ $t('roster.putfilehere') }}{{ $t('roster.selectfile') }}
{{ $t('roster.importServer') }} {{ $t('roster.downloadTemplate') }}

上传文件接口::action="uploadHost + '/user/importUserRank'"

uploadHost:是我自定义的路径

Vue脚本代码:

methods: {
   submitUpload() {
            // 将文件上传到服务器,先触发beforeUpload事件,对上传的文件进行校验,校验通过后才会上传
            this.$refs.upload.submit();
        },
    handleError(res, file) {
            // this.imageUrl = URL.createObjectURL(file.raw);
            this.$message('上传失败');
            this.showImport = false;
            this.$refs.upload.clearFiles();
        },
        handleSuccess(res, file) {
            // this.imageUrl = URL.createObjectURL(file.raw);
            this.$message('上传成功');
            if (res.invalidCount > 0) {//res.invalidCount接口返回的对象
                this.$message(res.invalid,10000);
            }
            this.showImport = false;
            this.$refs.upload.clearFiles();
        },
}

.net core接口代码

/// 
        /// 导入用户岗级
        /// 
        /// 
        /// 
        [HttpPost("importUserRank")]
        //[Permission(ModuleCodes.UserList, SimpleOperation.Import)]//模块验证和权限设置
        public Object ImportUserRankExcel(IFormFile file)
        {
            List invalidList = new List();
            List invalErrTxtList = new List();
            if (file?.Length > 0)
            {
                DataTable dt = ExcelHelper.ExcelToDatatable(file.OpenReadStream(), Path.GetExtension(file.FileName), fistRow: 0);
                if (dt.Rows.Count > 0)
                {
                    var importUserlist = ExcelHelper.ConvertTo(dt).ToList();
                    for (int i = 0; i < importUserlist.Count; i++)
                    {
                        var userReq = _userService.GetUserReqByOrgUserId(Convert.ToInt32(importUserlist[i].OrgUserId));
                        if (userReq == null)
                        {
                            invalidList.Add(i);
                            invalErrTxtList.Add(string.Format(_userlocalizer[UserResources.OrgUserIdNotExist], importUserlist[i].OrgUserId));
                            continue;
                        }
                        if (!_rankService.ExistRankCode(importUserlist[i].UserRank))
                        {
                            invalidList.Add(i);
                            invalErrTxtList.Add(string.Format(_ranklocalizer[RankResources.RankLevelCodeNotExist], importUserlist[i].UserRank));
                            continue;
                        }
                        userReq.UserRank = importUserlist[i].UserRank;
                        _userService.UpdateUser(userReq);
                    }
                }
            }

            return new
            {
                invalidCount = invalidList.Count,
                invalid = string.Join(";", invalErrTxtList),
            };
        }

你可能感兴趣的:(vue.js,.netcore,excel)