/// <summary> /// 校验Excel数据,这个方法一般用于重写校验逻辑 /// </summary> public virtual bool CheckImportData(string fileName, List <Spl_TestStudentsModel> list, ref ValidationErrors errors) { var targetFile = new FileInfo(fileName); if (!targetFile.Exists) { errors.Add("导入的数据文件不存在"); return(false); } var excelFile = new ExcelQueryFactory(fileName); //对应列头 excelFile.AddMapping <Spl_TestStudentsModel>(x => x.ClassId, "所属班级"); excelFile.AddMapping <Spl_TestStudentsModel>(x => x.Name, "姓名"); excelFile.AddMapping <Spl_TestStudentsModel>(x => x.Avg, "年龄"); excelFile.AddMapping <Spl_TestStudentsModel>(x => x.Photo, "头像"); excelFile.AddMapping <Spl_TestStudentsModel>(x => x.Enable, "状态"); excelFile.AddMapping <Spl_TestStudentsModel>(x => x.CreateTime, "创建时间"); //SheetName var excelContent = excelFile.Worksheet <Spl_TestStudentsModel>(0); int rowIndex = 1; //检查数据正确性 foreach (var row in excelContent) { var errorMessage = new StringBuilder(); var entity = new Spl_TestStudentsModel(); entity.Id = row.Id; entity.ClassId = row.ClassId; entity.Name = row.Name; entity.Avg = row.Avg; entity.Photo = row.Photo; entity.Enable = row.Enable; entity.CreateTime = row.CreateTime; //============================================================================= if (errorMessage.Length > 0) { errors.Add(string.Format( "第 {0} 列发现错误:{1}{2}", rowIndex, errorMessage, "<br/>")); } list.Add(entity); rowIndex += 1; } if (errors.Count > 0) { return(false); } return(true); }
public virtual Spl_TestStudentsModel GetById(object id) { if (IsExists(id)) { Spl_TestStudents entity = m_Rep.GetById(id); Spl_TestStudentsModel model = new Spl_TestStudentsModel(); model.Id = entity.Id; model.ClassId = entity.ClassId; model.Name = entity.Name; model.Avg = entity.Avg; model.Photo = entity.Photo; model.Enable = entity.Enable; model.CreateTime = entity.CreateTime; return(model); } else { return(null); } }
public virtual bool Create(ref ValidationErrors errors, Spl_TestStudentsModel model) { try { Spl_TestStudents entity = m_Rep.GetById(model.Id); if (entity != null) { errors.Add(Resource.PrimaryRepeat); return(false); } entity = new Spl_TestStudents(); entity.Id = model.Id; entity.ClassId = model.ClassId; entity.Name = model.Name; entity.Avg = model.Avg; entity.Photo = model.Photo; entity.Enable = model.Enable; entity.CreateTime = model.CreateTime; if (m_Rep.Create(entity)) { return(true); } else { errors.Add(Resource.InsertFail); return(false); } } catch (Exception ex) { errors.Add(ex.Message); ExceptionHander.WriteException(ex); return(false); } }
public virtual bool Edit(ref ValidationErrors errors, Spl_TestStudentsModel model) { try { Spl_TestStudents entity = m_Rep.GetById(model.Id); if (entity == null) { errors.Add(Resource.Disable); return(false); } entity.Id = model.Id; entity.ClassId = model.ClassId; entity.Name = model.Name; entity.Avg = model.Avg; entity.Photo = model.Photo; entity.Enable = model.Enable; entity.CreateTime = model.CreateTime; if (m_Rep.Edit(entity)) { return(true); } else { errors.Add(Resource.NoDataChange); return(false); } } catch (Exception ex) { errors.Add(ex.Message); ExceptionHander.WriteException(ex); return(false); } }