public IActionResult Create(TeenagerViewModel model) { var response = ResponseModelFactory.CreateInstance; using (_dbContext) { var entity = new HaikanSmartTownCockpit.Api.Entities.KeyYouthList(); entity.KeyYouthListUuid = Guid.NewGuid(); entity.Name = model.Name; entity.OwnedNetwork = model.OwnedNetwork; entity.Sex = model.Sex; entity.DateOfBirth = model.DateOfBirth; entity.IdCard = model.IdCard; entity.PersonType = model.PersonType; entity.Attention = model.Attention; entity.ResidenceAddress = model.ResidenceAddress; entity.RegisteredAddress = model.RegisteredAddress; entity.HouseholdRegistrationPoliceStation = model.HouseholdRegistrationPoliceStation; entity.NumberOfTheHouse = model.NumberOfTheHouse; entity.CurrentAddress = model.CurrentAddress; entity.NoRoomReason = model.NoRoomReason; entity.CurrentAddress1 = model.CurrentAddress1; entity.OtherAddress = model.OtherAddress; entity.FormerName = model.FormerName; entity.Employer = model.Employer; entity.ContactNumber = model.ContactNumber; entity.ContactPhone = model.ContactPhone; entity.Nation = model.Nation; entity.PoliticalStatus = model.PoliticalStatus; entity.Education = model.Education; entity.Occupation = model.Occupation; entity.MaritalStatus = model.MaritalStatus; entity.BloodType = model.BloodType; entity.ReligiousBelief = model.ReligiousBelief; entity.Height = model.Height; entity.Email = model.Email; entity.ServiceMembers = model.ServiceMembers; entity.LatestServiceHours = model.LatestServiceHours; entity.Remarks = model.Remarks; entity.IsDeleted = 0; _dbContext.KeyYouthList.Add(entity); int res = _dbContext.SaveChanges(); if (res > 0) { ToLog.AddLog("添加", "成功:添加:重点青少年人员信息一条数据", _dbContext); } response.SetSuccess("添加成功"); return(Ok(response)); } }
public IActionResult GetImport(IFormFile excelfile) { var response = ResponseModelFactory.CreateInstance; using (_dbContext) { DateTime beginTime = DateTime.Now; string sWebRootFolder = _hostingEnvironment.WebRootPath + "\\UploadFiles\\KeyPointExcel"; //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 { for (int i = 0; i < dt.Rows.Count; i++) { var entity = new HaikanSmartTownCockpit.Api.Entities.KeyYouthList(); entity.KeyYouthListUuid = Guid.NewGuid(); if (!string.IsNullOrEmpty(dt.Rows[i]["姓名"].ToString())) { entity.Name = 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())) { if (!string.IsNullOrEmpty(dt.Rows[i]["身份证"].ToString())) { string ddd = dt.Rows[i]["身份证"].ToString(); entity.IdCard = dt.Rows[i]["身份证"].ToString(); if (ddd != "" && ddd != null) { string sex = ""; //判断身份证是否18位 if (ddd.Length == 18) { sex = ddd.Substring(14, 3); if (sex != "" && sex != null) { if (int.Parse(sex) % 2 == 0) { entity.Sex = "女"; } else { entity.Sex = "男"; } } var d1 = ddd.Substring(6, 4) + "-" + ddd.Substring(10, 2) + "-" + ddd.Substring(12, 2); sex = ddd.Substring(14, 3); entity.DateOfBirth = d1; } //判断身份证是否15位 if (ddd.Length == 15) { sex = ddd.Substring(12, 3); if (sex != "" && sex != null) { if (int.Parse(sex) % 2 == 0) { entity.Sex = "女"; } else { entity.Sex = "男"; } } var d1 = "19" + ddd.Substring(6, 2) + "-" + ddd.Substring(8, 2) + "-" + ddd.Substring(10, 2); sex = ddd.Substring(12, 3); entity.DateOfBirth = d1; } } } } else { responsemsgdefault += "<p style='color:red'>" + "第" + (i + 2) + "行身份证号格式不正确" + "</p></br>"; defaultcount++; continue; } if (!string.IsNullOrEmpty(dt.Rows[i]["所属网路"].ToString())) { entity.OwnedNetwork = dt.Rows[i]["所属网路"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["人员类型"].ToString())) { entity.PersonType = dt.Rows[i]["人员类型"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["关注程度"].ToString())) { entity.Attention = dt.Rows[i]["关注程度"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["户籍地址"].ToString())) { entity.ResidenceAddress = dt.Rows[i]["户籍地址"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["户籍地详址"].ToString())) { entity.RegisteredAddress = dt.Rows[i]["户籍地详址"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["户籍派出所"].ToString())) { entity.HouseholdRegistrationPoliceStation = dt.Rows[i]["户籍派出所"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["房屋编号"].ToString())) { entity.NumberOfTheHouse = dt.Rows[i]["房屋编号"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["现住地址"].ToString())) { entity.CurrentAddress = dt.Rows[i]["现住地址"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["无房原因"].ToString())) { entity.NoRoomReason = dt.Rows[i]["无房原因"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["其他地址"].ToString())) { entity.OtherAddress = dt.Rows[i]["其他地址"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["曾用名"].ToString())) { entity.FormerName = dt.Rows[i]["曾用名"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["工作单位"].ToString())) { entity.Employer = dt.Rows[i]["工作单位"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["联系电话"].ToString())) { entity.ContactNumber = dt.Rows[i]["联系电话"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["联系手机"].ToString())) { entity.ContactPhone = dt.Rows[i]["联系手机"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["民族"].ToString())) { entity.Nation = dt.Rows[i]["民族"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["政治面貌"].ToString())) { entity.PoliticalStatus = dt.Rows[i]["政治面貌"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["文化程度"].ToString())) { entity.Education = dt.Rows[i]["文化程度"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["职业"].ToString())) { entity.Occupation = dt.Rows[i]["职业"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["婚姻状况"].ToString())) { entity.MaritalStatus = dt.Rows[i]["婚姻状况"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["血型"].ToString())) { entity.BloodType = dt.Rows[i]["血型"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["宗教信仰"].ToString())) { entity.ReligiousBelief = dt.Rows[i]["宗教信仰"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["身高"].ToString())) { entity.Height = dt.Rows[i]["身高"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["电子邮件"].ToString())) { entity.Email = dt.Rows[i]["电子邮件"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["有无服务成员"].ToString())) { entity.ServiceMembers = dt.Rows[i]["有无服务成员"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["最新服务时间"].ToString())) { entity.LatestServiceHours = dt.Rows[i]["最新服务时间"].ToString(); } if (!string.IsNullOrEmpty(dt.Rows[i]["备注"].ToString())) { entity.Remarks = dt.Rows[i]["备注"].ToString(); } entity.IsDeleted = 0; _dbContext.KeyYouthList.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)); } } }