public async Task <IActionResult> OnPostImportAsync() { List <CreateUpdateContactDto> createUpdateContactDtos = new List <CreateUpdateContactDto>(); IFormFile excel = Request.Form.Files[0]; if (excel == null) { return(NoContent()); } using (var workbook = new XLWorkbook(excel.OpenReadStream())) { var worksheet = workbook.Worksheet("Contact"); var count = 0; foreach (var row in worksheet.Rows()) { count += 1; if (count > 1) //skip the first row. { if (row.Cell(1).Value.Equals("") || row.Cell(2).Equals("") || row.Cell(3).Equals("") || row.Cell(4).Equals("") || row.Cell(5).Equals(6)) { break; } ContactDtos.Add( new ContactDto() { //GroupName = row.Cell(1).Value.ToString(), Email = row.Cell(2).Value.ToString(), FirstName = row.Cell(3).Value.ToString(), LastName = row.Cell(4).Value.ToString(), DateOfBirth = row.Cell(5).Value.To <DateTime>(), PhoneNumber = row.Cell(6).Value == null ? "" : row.Cell(6).Value.ToString(), Addition = row.Cell(7).Value == null ? "" : row.Cell(7).Value.ToString(), Status = row.Cell(8).Value.Equals("") ? 0 : row.Cell(8).Value.To <int>() } ); createUpdateContactDtos.Add( new CreateUpdateContactDto() { //GroupId = _groupRepository.FindByNameAsync(row.Cell(1).Value.ToString()).Result.Id, Email = row.Cell(2).Value.ToString(), FirstName = row.Cell(3).Value.ToString(), LastName = row.Cell(4).Value.ToString(), DateOfBirth = row.Cell(5).Value.To <DateTime>(), PhoneNumber = row.Cell(6).Value == null ? "" : row.Cell(6).Value.ToString(), Addition = row.Cell(7).Value == null ? "" : row.Cell(7).Value.ToString(), Status = row.Cell(8).Value.Equals("") ? 0 : row.Cell(8).Value.To <int>() } ); } } foreach (CreateUpdateContactDto createUpdateContactDto in createUpdateContactDtos) { await _contactAppService.CreateAsync(createUpdateContactDto); } //View result ajax StringBuilder sb = new StringBuilder(); sb.Append("<table class='table table-bordered'><tr>"); sb.Append("<th>Group Contact</th>"); sb.Append("<th>Email</th>"); sb.Append("<th>FirstName</th>"); sb.Append("<th>LastName</th>"); sb.Append("<th>DateOfBirth</th>"); sb.Append("<th>PhoneNumber</th>"); sb.Append("</tr>"); sb.AppendLine("<tr>"); foreach (ContactDto contactDto in ContactDtos) { //sb.Append("<td>" + contactDto.GroupName + "</td>"); sb.Append("<td>" + contactDto.Email + "</td>"); sb.Append("<td>" + contactDto.FirstName + "</td>"); sb.Append("<td>" + contactDto.LastName + "</td>"); sb.Append("<td>" + contactDto.DateOfBirth + "</td>"); sb.Append("<td>" + contactDto.PhoneNumber + "</td>"); sb.AppendLine("</tr>"); } sb.Append("</table>"); return(this.Content(sb.ToString())); } }
public async Task <IActionResult> OnPostImportAsync() { List <CreateUpdateContactDto> createUpdateContactDtos = new List <CreateUpdateContactDto>(); IFormFile excel = Request.Form.Files[0]; if (excel == null) { return(NoContent()); } StringBuilder sb = new StringBuilder(); sb.Append("<table class='table table-bordered table-hover'><tr>"); sb.Append("<th>Email</th>"); sb.Append("<th>First Name</th>"); sb.Append("<th>Last Name</th>"); sb.Append("<th>Date of birth</th>"); sb.Append("<th>PhoneNumber</th>"); sb.Append("<th>Addition</th>"); sb.Append("</tr>"); using (var workbook = new XLWorkbook(excel.OpenReadStream())) { var worksheet = workbook.Worksheet("Contact"); var count = 0; foreach (var row in worksheet.Rows()) { count += 1; if (count > 1) //skip the first row. { if (row.Cell(1).Value.Equals("") || row.Cell(2).Value.Equals("") || row.Cell(3).Value.Equals("") || row.Cell(4).Value.Equals("")) { break; } var groupName = row.Cell(7).Value.ToString(); var group = await _groupRepository.FindByNameAsync(groupName); var createContactDto = new CreateUpdateContactDto() { GroupIds = new List <Guid>() { group.Id }, Email = row.Cell(1).Value.ToString(), FirstName = row.Cell(2).Value.ToString(), LastName = row.Cell(3).Value.ToString(), DateOfBirth = row.Cell(4).Value.To <DateTime>(), PhoneNumber = row.Cell(5).Value == null ? "" : row.Cell(5).Value.ToString(), Addition = row.Cell(6).Value == null ? "" : row.Cell(6).Value.ToString(), Status = 0 }; try { await _contactAppService.CreateAsync( createContactDto ); sb.AppendLine("<tr>"); sb.Append("<td>" + createContactDto.Email + "</td>"); sb.Append("<td>" + createContactDto.FirstName + "</td>"); sb.Append("<td>" + createContactDto.LastName + "</td>"); sb.Append("<td>" + createContactDto.DateOfBirth + "</td>"); sb.Append("<td>" + createContactDto.PhoneNumber + "</td>"); sb.Append("<td>" + createContactDto.Addition + "</td>"); sb.AppendLine("</tr>"); } catch (Exception ex) { sb.AppendLine($"<tr class='table-danger'>"); sb.Append("<td>" + createContactDto.Email + "</td>"); sb.Append("<td>" + createContactDto.FirstName + "</td>"); sb.Append("<td>" + createContactDto.LastName + "</td>"); sb.Append("<td>" + createContactDto.DateOfBirth + "</td>"); sb.Append("<td>" + createContactDto.PhoneNumber + "</td>"); sb.Append("<td>" + createContactDto.Addition + "</td>"); sb.AppendLine("</tr>"); Console.WriteLine(ex); } } } //View result ajax sb.Append("</table>"); return(this.Content(sb.ToString())); } }