public async Task SaveData(PB_TrayType data) { if (data.Id.IsNullOrEmpty()) { InitEntity(data); await _pB_TrayTypeBus.AddDataAsync(data); } else { await _pB_TrayTypeBus.UpdateDataAsync(data); } }
public async Task <AjaxResult> Import(IFormFile file)// file { string ReturnValue = string.Empty; //定义一个bool类型的变量用来做验证 bool flag = true; try { #region 检查文件 string fileExt = Path.GetExtension(file.FileName).ToLower(); //定义一个集合一会儿将数据存储进来,全部一次丢到数据库中保存 var Data = new List <PB_TrayType>(); MemoryStream ms = new MemoryStream(); file.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); IWorkbook book; if (fileExt == ".xlsx") { book = new XSSFWorkbook(ms); } else if (fileExt == ".xls") { book = new HSSFWorkbook(ms); } else { book = null; } ISheet sheet = book.GetSheetAt(0); int CountRow = sheet.LastRowNum + 1;//获取总行数 if (CountRow - 1 == 0) { return(Error("Excel列表数据项为空!")); } #endregion #region 循环验证 for (int i = 1; i < CountRow; i++) { //获取第i行的数据 var row = sheet.GetRow(i); if (row != null) { //循环的验证单元格中的数据 for (int j = 0; j < 6; j++) { if ((j == 4 || j == 5) || (row.GetCell(j) == null || row.GetCell(j).ToString().Trim().Length == 0)) { //return Error(ReturnValue += $"注意第{i + 1}行,第{j + 1}列数据为空。"); } else if (row.GetCell(j) == null || row.GetCell(j).ToString().Trim().Length == 0) { flag = false; return(Error(ReturnValue += $"第{i + 1}行,第{j + 1}列数据不能为空。")); } } } } #endregion if (flag) { for (int i = 1; i < CountRow; i++)// { //实例化实体对象 PB_TrayType commodity = new PB_TrayType(); var row = sheet.GetRow(i); if (row.GetCell(0) != null && row.GetCell(0).ToString().Trim().Length > 0) { commodity.Id = IdHelper.GetId(); commodity.CreatorId = _Op.UserId; //"Admin";//_Op.UserId; commodity.IsZone = false; //默认不启用分区 commodity.Code = row.GetCell(0).ToString(); } if (row.GetCell(1) != null && row.GetCell(1).ToString().Trim().Length > 0) { commodity.Name = row.GetCell(1).ToString(); } if (row.GetCell(2) != null && row.GetCell(2).ToString().Trim().Length > 0) { commodity.Length = Convert.ToDouble(row.GetCell(2).ToString()); } if (row.GetCell(3) != null && row.GetCell(3).ToString().Trim().Length > 0) { commodity.Width = Convert.ToDouble(row.GetCell(3).ToString()); } if (row.GetCell(4) != null && row.GetCell(4).ToString().Trim().Length > 0) { commodity.High = Convert.ToDouble(row.GetCell(4).ToString()); } Data.Add(commodity); } foreach (var item in Data) { if (item.Length == null) { item.Length = null; } if (item.Width == null) { item.Width = null; } if (item.High == null) { item.High = null; } } if (Data.Count > 0) { int j = 1000; for (int i = 0; i < Data.Count; i += 1000) { var cList = new List <PB_TrayType>(); cList = Data.Take(j).Skip(i).ToList(); j += 1000; await _pB_TrayTypeBus.AddDataExlAsync(cList); } ReturnValue = $"数据导入成功,共导入{CountRow - 1}条数据。"; } } if (!flag) { return(Error(ReturnValue = "数据存在问题!" + ReturnValue)); } } catch (Exception) { return(Error("数据异常!")); } return(Success(ReturnValue)); }