public IActionResult UserInfoCreate(UsreInfoViewModel model) { var response = ResponseModelFactory.CreateInstance; using (_dbContext) { var entity = new HaikanSmartTownCockpit.Api.Entities.Sightseer(); entity.SightseerUuid = Guid.NewGuid(); entity.SightseerName = model.SightseerName; entity.Sex = model.Sex; entity.Nation = model.Nation; entity.Phone = model.Phone; entity.IdentityCard = model.IdentityCard; entity.Laiyuandi = model.Laiyuandi; entity.Age = model.Age; entity.Shengneiwai = model.Shengneiwai; //entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd"); //entity.AddPeople = model.addPeople; entity.IsDeleted = 0; entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd"); entity.AddPeople = AuthContextService.CurrentUser.DisplayName; _dbContext.Sightseer.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.Sightseer(); entity.SightseerUuid = Guid.NewGuid(); Regex regxm = new Regex("^([\\u4e00-\\u9fa5]){2,7}$"); if (regxm.IsMatch(dt.Rows[i]["姓名"].ToString())) { entity.SightseerName = 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 regsfz = new Regex("^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$"); if (regsfz.IsMatch(dt.Rows[i]["身份证号"].ToString())) { entity.IdentityCard = dt.Rows[i]["身份证号"].ToString(); } else { responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行身份证号格式不正确" + "</p></br>"; defaultcount++; continue; } Regex regnl = new Regex("^[0-9]*[1-9][0-9]*$"); if (regnl.IsMatch(dt.Rows[i]["年龄"].ToString())) { entity.Age = 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.Laiyuandi = dt.Rows[i]["来源地"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["手机"].ToString())) { entity.Phone = dt.Rows[i]["手机"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["是否省内人员"].ToString())) { Regex regsnw = new Regex("^(是|否)$"); if (regsnw.IsMatch(dt.Rows[i]["是否省内人员"].ToString())) { entity.Shengneiwai = dt.Rows[i]["是否省内人员"].ToString(); } else { responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行格式不正确,应为:是|否" + "</p></br>"; defaultcount++; continue; } } else { entity.Shengneiwai = "是"; } entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd"); entity.AddPeople = AuthContextService.CurrentUser.DisplayName; entity.IsDeleted = 0; _dbContext.Sightseer.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)); } } }