public async Task SaveData(PB_Supplier data) { if (data.Id.IsNullOrEmpty()) { InitEntity(data); if (data.Code.IsNullOrWhiteSpace()) { data.Code = await _provider.GetRequiredService <IPB_BarCodeTypeBusiness>().Generate("PB_Supplier"); } await _pB_SupplierBus.AddDataAsync(data); } else { await _pB_SupplierBus.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_Supplier>(); 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_Supplier commodity = new PB_Supplier(); 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.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.Type = row.GetCell(2).ToString(); } if (row.GetCell(3) != null && row.GetCell(3).ToString().Trim().Length > 0) { commodity.Phone = row.GetCell(3).ToString(); } if (row.GetCell(4) != null && row.GetCell(4).ToString().Trim().Length > 0) { commodity.Fax = row.GetCell(4).ToString(); } if (row.GetCell(5) != null && row.GetCell(5).ToString().Trim().Length > 0) { commodity.Email = row.GetCell(5).ToString(); } if (row.GetCell(6) != null && row.GetCell(6).ToString().Trim().Length > 0) { commodity.ContactName = row.GetCell(6).ToString(); } if (row.GetCell(7) != null && row.GetCell(7).ToString().Trim().Length > 0) { commodity.Address = row.GetCell(7).ToString(); } Data.Add(commodity); } foreach (var item in Data) { if (item.Phone == null) { item.Phone = null; } if (item.Fax == null) { item.Fax = null; } if (item.Email == null) { item.Email = null; } if (item.ContactName == null) { item.ContactName = null; } if (item.Address == null) { item.Address = null; } } if (Data.Count > 0) { int j = 1000; for (int i = 0; i < Data.Count; i += 1000) { var cList = new List <PB_Supplier>(); cList = Data.Take(j).Skip(i).ToList(); j += 1000; await _pB_SupplierBus.AddDataExlAsync(cList); } ReturnValue = $"数据导入成功,共导入{CountRow - 1}条数据。"; } } if (!flag) { return(Error(ReturnValue = "数据存在问题!" + ReturnValue)); } } catch (Exception) { return(Error("数据异常!")); } return(Success(ReturnValue)); }