public IActionResult ImportaDependentes(IFormFile importExcel) { DependenteViewModel cli = new DependenteViewModel(); if (importExcel == null || importExcel.Length == 0) { return(View()); } using (var memoryStream = new MemoryStream()) { importExcel.CopyToAsync(memoryStream).ConfigureAwait(false); using (var package = new ExcelPackage(memoryStream)) { for (int i = 1; i <= package.Workbook.Worksheets.Count; i++) { var totalRows = package.Workbook.Worksheets[i].Dimension?.Rows; var totalCollumns = package.Workbook.Worksheets[i].Dimension?.Columns; for (int j = 1; j <= totalRows; j++) { for (int k = 1; k <= totalCollumns.Value; k++) { var cpf = package.Workbook.Worksheets[i].Cells[j, k].Value == null ? "x" : package.Workbook.Worksheets[i].Cells[j, k].Value.ToString(); cli.Nome = package.Workbook.Worksheets[i].Cells[j, 2].Value == null ? "x" : package.Workbook.Worksheets[i].Cells[j, 2].Value.ToString(); cli.Parentesco = package.Workbook.Worksheets[i].Cells[j, 3].Value == null ? "x" : package.Workbook.Worksheets[i].Cells[j, 3].Value.ToString(); cli.Participacao = package.Workbook.Worksheets[i].Cells[j, 4].Value == null ? 0 : Convert.ToDecimal(package.Workbook.Worksheets[i].Cells[j, 4].Value); if (cpf.Length == 10) { cpf = 0 + cpf; } k = 21; ClienteViewModel cliente = new ClienteViewModel(); cliente = _clienteAppService.ObterPorCpf(cpf); if (cliente != null) { cli.ClienteId = cliente.Id; _clienteAppService.AdicionarDependente(cli); } cli.Id = Guid.NewGuid(); } } } } } ViewBag.RetornoPost = OperacaoValida() ? "succes, Cliente Registrado com Sucesso!" : "error, Cliente não pode ser Registrado, verifique as mensagens"; if (OperacaoValida()) { return(RedirectToAction("Index")); } else { return(View()); } }