4

Tenho seguinte código:

        [HttpPost]
        public ActionResult importCSV(HttpPostedFileBase file)
        {
            if (file.ContentLength <= 0)
            {
                ViewBag.Message = "Nenhum arquivo foi enviado.";
                return View("index");
            }

                try
                {
                    string arquivo = Path.Combine(Server.MapPath("/ArquivosImportados"), Path.GetFileName(file.FileName));

                    string tipo = Path.GetExtension(arquivo);

                    if (tipo == ".csv")
                    {
                        file.SaveAs(arquivo);
                        ViewBag.Message = "Sucesso ao enviar arquivo! " + tipo;
                    }
                    else
                    {
                        ViewBag.Message = "O arquivo enviado não foi reconhecido com .csv válido!";
                    }

                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERRO: " + ex.Message.ToString();
                }

            return View("index");
        }

É possível ler o arquivo CSV sem precisar salva-lo? Como posso realizar a leitura?

1 Resposta 1

3

Já experimentou fazer isso:

StreamReader csvreader = new StreamReader(file.InputStream);

while (!csvreader.EndOfStream)
{
    var linha = csvreader.ReadLine();
    var valores = linha.Split(';');

    // o que você precisa fazer aqui
}
1

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .