public async Task <IActionResult> Importar([FromForm] ImportarAlunos importarAlunos) { AppResponse resposta = await _usuarioServico.ImportarAlunos(importarAlunos); if (resposta.Sucesso) { return(Ok(resposta)); } return(BadRequest(resposta)); }
public async Task <AppResponse> Importar(ImportarAlunos request) { var content = new MultipartFormDataContent(); var imagemBytes = new ByteArrayContent(FileHelper.ConvertToBytes(request.Arquivo)); imagemBytes.Headers.Add("content-type", "application/vnd.ms-excel"); content.Add(imagemBytes, BotarAspas("arquivo"), BotarAspas("excel-importacao.xlsx")); HttpResponseMessage resposta = await httpClient.PostAsync("Disciplinas/Importar", content); AppResponse conteudoResposta = await resposta.Content.ReadFromJsonAsync <AppResponse>(); return(conteudoResposta); }
public async Task <AppResponse> ImportarAlunos(ImportarAlunos importarAlunos) { var erros = new List <string>(); if (importarAlunos.Arquivo.Length == 0) { return(new AppResponse(false, "Arquivo não informado ou corrompido.")); } else { // copiando arquivo... var filePath = Path.GetTempFileName(); string destino = Path.Combine(_hostingEnvironment.WebRootPath, "Importacoes", Guid.NewGuid() + ".xlsx"); using (var stream = new FileStream(destino, FileMode.Create)) await importarAlunos.Arquivo.CopyToAsync(stream); Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR"); IWorkbook workbook = new XSSFWorkbook(); ISheet sheet; using (var stream = new FileStream(destino, FileMode.Open)) { stream.Position = 0; XSSFWorkbook xssWorkbook = new XSSFWorkbook(stream); sheet = xssWorkbook.GetSheetAt(0); } int linha = 0; var usuarios = new List <CriarAlunoRequest>(); foreach (IRow row in sheet) { if (linha == 0) { linha++; continue; } string nome = row.GetCell(0).ToString(); string login = row.GetCell(1).ToString(); string email = row.GetCell(2).ToString(); string matricula = row.GetCell(3).ToString(); var usuario = new CriarAlunoRequest(nome, login, email, "123456", new List <int>()) { Matricula = matricula }; usuarios.Add(usuario); linha++; } foreach (var criarAlunoRequest in usuarios) { AppResponse resposta = await CriarAluno(criarAlunoRequest); if (!resposta.Sucesso) { erros.Add(string.Join(",", resposta.Erros)); } } } if (erros.Count == 0) { return(new AppResponse(true, "Importação realizada com sucesso.")); } else { return(new AppResponse(true, $"Importação realizada, porem ocorreram alguns erros:\n {string.Join("\n",erros)}")); } }