public IActionResult DangJibenInfoCreate(DangJibenInfoViewModel model) { var response = ResponseModelFactory.CreateInstance; using (_dbContext) { var entity = new HaikanSmartTownCockpit.Api.Entities.CpcmanInfo(); entity.CpcmanUuid = Guid.NewGuid(); entity.RealName = model.RealName; entity.Sex = model.Sex; entity.Education = model.Education; entity.DangOrganizationName = model.DangOrganizationName; entity.Politics = model.Politics; if (model.Birth != null && model.Birth != "") { entity.Birth = DateTime.Parse(model.Birth); //DateTime now = DateTime.Now; //DateTime birth = Convert.ToDateTime(model.Birth); //int age = now.Year - birth.Year; //if (now.Month < birth.Month || (now.Month == birth.Month && now.Day < birth.Day)) //{ // age--; //} //entity.Age = age < 0 ? 0 : age; } entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd"); entity.AddPeople = AuthContextService.CurrentUser.DisplayName; //entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd"); //entity.AddPeople = model.addPeople; entity.IsDeleted = 0; _dbContext.CpcmanInfo.Add(entity); int res = _dbContext.SaveChanges(); if (res > 0) { ToLog.AddLog("添加", "成功:添加:党员信息一条数据", _dbContext); } response.SetSuccess("添加成功"); return(Ok(response)); } }
public IActionResult DangJibenInfoImport(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}.xls"; 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(), "党员信息", true); if (dt == null || dt.Rows.Count == 0) { response.SetFailed("表格无数据"); return(Ok(response)); } else { for (int i = 0; i < dt.Rows.Count; i++) { var entity = new HaikanSmartTownCockpit.Api.Entities.CpcmanInfo(); entity.CpcmanUuid = Guid.NewGuid(); if (!string.IsNullOrEmpty(dt.Rows[i]["序号"].ToString())) { entity.TableNum = dt.Rows[i]["序号"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["党组织"].ToString())) { entity.DangOrganizationName = dt.Rows[i]["党组织"].ToString(); } Regex regmm = new Regex("^(正式党员|预备党员|入党积极分子|入党申请人)$"); if (regmm.IsMatch(dt.Rows[i]["面貌"].ToString())) { entity.Politics = 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; } if (!string.IsNullOrEmpty(dt.Rows[i]["姓名"].ToString())) { entity.RealName = dt.Rows[i]["姓名"].ToString(); } else { responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行姓名不正确" + "</p></br>"; defaultcount++; continue; } //Regex regbr = new Regex("^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$"); //if (regbr.IsMatch(dt.Rows[i]["出生日期"].ToString())) //{ // entity.Birth = (DateTime?)dt.Rows[i]["出生日期"]; // DateTime now = DateTime.Now; // DateTime birth = Convert.ToDateTime(entity.Birth); // int age = now.Year - birth.Year; // if (now.Month < birth.Month || (now.Month == birth.Month && now.Day < birth.Day)) // { // age--; // } // entity.Age = age < 0 ? 0 : age; //} //else //{ // responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行出生日期不正确,例:1999-09-09" + "</p></br>"; // defaultcount++; // continue; //} //Regex reg1 = new Regex("^(小学|初中|职高|高中|大专|大学|研究生)$"); //if (reg1.IsMatch(dt.Rows[i]["学历"].ToString())) //{ // entity.Education = dt.Rows[i]["学历"].ToString(); //} //else //{ // responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行学历不正确,应为:小学|初中|职高|高中|大专|大学|研究生" + "</p></br>"; // defaultcount++; // continue; //} if (!string.IsNullOrEmpty(dt.Rows[i]["学历"].ToString())) { entity.Education = dt.Rows[i]["学历"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["出生年月"].ToString())) { entity.Birth = DateTime.Parse(dt.Rows[i]["出生年月"].ToString()); //DateTime now = DateTime.Now; //DateTime birth = Convert.ToDateTime(entity.Birth); //int age = now.Year - birth.Year; //if (now.Month < birth.Month || (now.Month == birth.Month && now.Day < birth.Day)) //{ // age--; //} //entity.Age = age < 0 ? 0 : age; } entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd"); entity.AddPeople = AuthContextService.CurrentUser.DisplayName; //entity.AddTime = DateTime.Now.ToString("yyyy-MM-dd"); //entity.AddPeople = AuthContextService.CurrentUser.DisplayName; entity.IsDeleted = 0; _dbContext.CpcmanInfo.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)); } } }