public IActionResult UserInfoCreate(FollowvillagerViewModle model) { var response = ResponseModelFactory.CreateInstance; using (_dbContext) { var entity = new HaikanSmartTownCockpit.Api.Entities.Followvillager(); entity.FollowvillagerUuid = Guid.NewGuid(); entity.FollowvillagerName = model.FollowvillagerName; entity.Sex = model.Sex; entity.Nation = model.Nation; entity.Phone = model.Phone; entity.IdentityCard = model.IdentityCard; entity.Shiji = model.Shiji; entity.Jieshao = model.Jieshao; entity.IsDeleted = 0; entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd"); entity.AddPeople = AuthContextService.CurrentUser.DisplayName; _dbContext.Followvillager.Add(entity); int res = _dbContext.SaveChanges(); if (res > 0) { ToLog.AddLog("添加", "成功:添加:乡贤信息一条数据", _dbContext); } response.SetSuccess("添加成功"); return(Ok(response)); } }
public IActionResult UserInfoImport(IFormFile excelfile) { var response = ResponseModelFactory.CreateInstance; using (_dbContext) { DateTime beginTime = DateTime.Now; string sWebRootFolder = _hostingEnvironment.WebRootPath + "\\UploadFiles\\ImportUserInfoExcel"; //var schoolinfo = _dbContext.SchoolInforManagement.AsQueryable(); string uploadtitle = " 乡贤信息导入" + DateTime.Now.ToString("yyyyMMddHHmmss"); string sFileName = $"{uploadtitle}.xlsx"; FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName)); //string conStr = ConnectionStrings.DefaultConnection; string responsemsgsuccess = ""; string responsemsgrepeat = ""; string responsemsgdefault = ""; int successcount = 0; int repeatcount = 0; int defaultcount = 0; string today = DateTime.Now.ToString("yyyy-MM-dd"); try { //把excelfile中的数据复制到file中 using (FileStream fs = new FileStream(file.ToString(), FileMode.Create)) //初始化一个指定路径和创建模式的FileStream { excelfile.CopyTo(fs); fs.Flush(); //清空stream的缓存,并且把缓存中的数据输出到file } DataTable dt = Haikan3.Utils.ExcelTools.ExcelToDataTable(file.ToString(), "Sheet1", true); if (dt == null || dt.Rows.Count == 0) { response.SetFailed("表格无数据"); return(Ok(response)); } else { if (!dt.Columns.Contains("姓名")) { response.SetFailed("无‘姓名’列"); return(Ok(response)); } for (int i = 0; i < dt.Rows.Count; i++) { var entity = new HaikanSmartTownCockpit.Api.Entities.Followvillager(); entity.FollowvillagerUuid = Guid.NewGuid(); Regex regxm = new Regex("^([\\u4e00-\\u9fa5]){2,7}$"); if (regxm.IsMatch(dt.Rows[i]["姓名"].ToString())) { entity.FollowvillagerName = dt.Rows[i]["姓名"].ToString(); } else { responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行姓名格式不正确" + "</p></br>"; defaultcount++; continue; } Regex regxb = new Regex("^(男|女)$"); if (regxb.IsMatch(dt.Rows[i]["性别"].ToString())) { entity.Sex = dt.Rows[i]["性别"].ToString(); } else { responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行性别不正确" + "</p></br>"; defaultcount++; continue; } //Regex reg = new Regex("^(汉族|蒙古族|回族|藏族|维吾尔族|苗族|彝族|壮族|布依族|朝鲜族|满族|侗族|瑶族|白族|土家族|哈尼族|哈萨克族|傣族|黎族|僳僳族|佤族|畲族|高山族|拉祜族|水族|东乡族|纳西族|景颇族|柯尔克孜族|土族|达斡尔族|仫佬族|羌族|布朗族|撒拉族|毛南族|仡佬族|锡伯族|阿昌族|普米族|塔吉克族|怒族|乌孜别克族|俄罗斯族|鄂温克族|德昂族|保安族|京族|独龙族|鄂伦春族|赫哲族|裕固族|门巴族|珞巴族|基诺族)$"); //if (reg.IsMatch(dt.Rows[i]["民族"].ToString())) //{ // entity.Nation = dt.Rows[i]["民族"].ToString(); //} //else //{ // responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行民族不正确" + "</p></br>"; // defaultcount++; // continue; //} if (!string.IsNullOrEmpty(dt.Rows[i]["手机"].ToString())) { entity.Phone = dt.Rows[i]["手机"].ToString(); } entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd"); entity.AddPeople = AuthContextService.CurrentUser.DisplayName; entity.IsDeleted = 0; _dbContext.Followvillager.Add(entity); _dbContext.SaveChanges(); successcount++; } } responsemsgsuccess = "<p style='color:green'>导入成功:" + successcount + "条</p></br>" + responsemsgsuccess; responsemsgrepeat = "<p style='color:orange'>重复需手动修改数据:" + repeatcount + "条</p></br>" + responsemsgrepeat; responsemsgdefault = "<p style='color:red'>导入失败:" + defaultcount + "条</p></br>" + responsemsgdefault; ToLog.AddLog("导入", "成功:导入:乡贤信息数据", _dbContext); DateTime endTime = DateTime.Now; TimeSpan useTime = endTime - beginTime; string taketime = "导入时间" + useTime.TotalSeconds.ToString() + "秒 "; response.SetData(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(new { time = taketime, successmsg = responsemsgsuccess , repeatmsg = responsemsgrepeat, defaultmsg = responsemsgdefault }))); return(Ok(response)); } catch (Exception ex) { response.SetFailed(ex.Message); return(Ok(response)); } } }