public async Task <ActionResult> Import(IFormFile file) { if (!ModelState.IsValid) { throw new BindException(ModelState); } StatusFilter StatusFilter = new StatusFilter { Skip = 0, Take = int.MaxValue, Selects = StatusSelect.ALL }; List <Status> Statuses = await StatusService.List(StatusFilter); List <TicketType> TicketTypes = new List <TicketType>(); using (ExcelPackage excelPackage = new ExcelPackage(file.OpenReadStream())) { ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.FirstOrDefault(); if (worksheet == null) { return(Ok(TicketTypes)); } int StartColumn = 1; int StartRow = 1; int IdColumn = 0 + StartColumn; int CodeColumn = 1 + StartColumn; int NameColumn = 2 + StartColumn; int ColorCodeColumn = 3 + StartColumn; int StatusIdColumn = 4 + StartColumn; int UsedColumn = 8 + StartColumn; for (int i = StartRow; i <= worksheet.Dimension.End.Row; i++) { if (string.IsNullOrEmpty(worksheet.Cells[i + StartRow, StartColumn].Value?.ToString())) { break; } string IdValue = worksheet.Cells[i + StartRow, IdColumn].Value?.ToString(); string CodeValue = worksheet.Cells[i + StartRow, CodeColumn].Value?.ToString(); string NameValue = worksheet.Cells[i + StartRow, NameColumn].Value?.ToString(); string ColorCodeValue = worksheet.Cells[i + StartRow, ColorCodeColumn].Value?.ToString(); string StatusIdValue = worksheet.Cells[i + StartRow, StatusIdColumn].Value?.ToString(); string UsedValue = worksheet.Cells[i + StartRow, UsedColumn].Value?.ToString(); TicketType TicketType = new TicketType(); TicketType.Code = CodeValue; TicketType.Name = NameValue; TicketType.ColorCode = ColorCodeValue; Status Status = Statuses.Where(x => x.Id.ToString() == StatusIdValue).FirstOrDefault(); TicketType.StatusId = Status == null ? 0 : Status.Id; TicketType.Status = Status; TicketTypes.Add(TicketType); } } TicketTypes = await TicketTypeService.Import(TicketTypes); if (TicketTypes.All(x => x.IsValidated)) { return(Ok(true)); } else { List <string> Errors = new List <string>(); for (int i = 0; i < TicketTypes.Count; i++) { TicketType TicketType = TicketTypes[i]; if (!TicketType.IsValidated) { string Error = $"Dòng {i + 2} có lỗi:"; if (TicketType.Errors.ContainsKey(nameof(TicketType.Id))) { Error += TicketType.Errors[nameof(TicketType.Id)]; } if (TicketType.Errors.ContainsKey(nameof(TicketType.Code))) { Error += TicketType.Errors[nameof(TicketType.Code)]; } if (TicketType.Errors.ContainsKey(nameof(TicketType.Name))) { Error += TicketType.Errors[nameof(TicketType.Name)]; } if (TicketType.Errors.ContainsKey(nameof(TicketType.ColorCode))) { Error += TicketType.Errors[nameof(TicketType.ColorCode)]; } if (TicketType.Errors.ContainsKey(nameof(TicketType.StatusId))) { Error += TicketType.Errors[nameof(TicketType.StatusId)]; } if (TicketType.Errors.ContainsKey(nameof(TicketType.Used))) { Error += TicketType.Errors[nameof(TicketType.Used)]; } Errors.Add(Error); } } return(BadRequest(Errors)); } }