Save information list in txt MVC

Asked

Viewed 138 times

-1

Good afternoon guys, as I am new in mvc would like a help if possible from you, I am trying to get some data from my list add the same in a txt file and save on my machine, what this agreeing is that the file is generated in my c:However, I am not able to write the information that comes from my list inside the archive, could if possible give me a help. Here’s an example from my controller.

        public IActionResult Exportar(DateTime dateTime, DateTime dateTimeFinal)
        {


            var Layout = _context.RegistroDePontos.Where(e => e.data >= dateTime && e.data <= dateTimeFinal).ToList();
                                                  
            string nomeArquivo = @"C:\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
            StreamWriter writer = new StreamWriter(nomeArquivo, true);
            
            for(int i = 0; i < Layout.Count; i++)

                 writer.WriteLine(Layout[i].ToString());
            
            writer.Close();
           
            return View("Exportar",Layout);
        }

  • you want to write the file to the server or return it filled to the user who is using your application?

  • There’s no "nothing" wrong with your program... or you don’t have permission to write or your context.RegistroDePontos is not returning anything in the conditions of Where()...

  • In the point record the property returns only the Date or a Date and Time?

  • Leandro, Thanks for your fedback, I managed to solve problem. Vlewww

  • If the solution was not for my answer, the ideal is for you to post an answer with your resolution. But if it was, it would be nice for you to mark as accepted so that the question is completed and this can be useful for other users.

1 answer

0


The problem is that you are trying to write an object as a string without telling you what exactly you want to present in the file. See below for an example with a fictitious construction.

public class RegistroDePonto
{
    public string Nome { get; set; }
    public DateTime Data { get; set; }
    public string Tipo { get; set; }
}

Example of reading the collection and writing to the file

//simulando a sua consulta
var layout = new List<RegistroDePontos>
{
    new RegistroDePontos { Nome = "Fulano", Data = DateTime.Parse("2020-03-27 08:30:00"), Tipo = "ENTRADA"},
    new RegistroDePontos { Nome = "Fulano", Data = DateTime.Parse("2020-03-27 12:00:00"), Tipo = "INTERVALO"},
    new RegistroDePontos { Nome = "Fulano", Data = DateTime.Parse("2020-03-27 13:00:00"), Tipo = "RETORNO"},
    new RegistroDePontos { Nome = "Fulano", Data = DateTime.Parse("2020-03-27 17:30:00"), Tipo = "SAÍDA"}
};

string nomeArquivo = @"C:\Temp\teste_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";

using (StreamWriter writer = new StreamWriter(nomeArquivo, true))
{
    foreach (var registro in layout)
        writer.WriteLine($"{registro.Data} - {registro.Nome} - {registro.Tipo}");
}

Output:

27/03/2020 08:30:00 - Fulano - ENTRADA 
27/03/2020 12:00:00 - Fulano - INTERVALO 
27/03/2020 13:00:00 - Fulano - RETORNO 
27/03/2020 17:30:00 - Fulano - SAÍDA
  • Thanks friend for your help. Hug...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.