Generate csv file in Excel standard with Csvhelper C#

Asked

Viewed 76 times

1

I created a project to generate a . csv file (which will be used in Excel). So far it is working normally and is being generated in the format "Excel . csv", but the problem is that when opening the file (by Excel) it is not separated in the columns as table. Getting everything in a column. Example in the image

I believe the problem lies in the delimiter because if I generate a csv manually in Excel and read by a text editor, the information will be separated by point and comma and not by comma (which is how it’s getting in my case).

I have tried changing the delimiter (where the default is ","), but the property cannot be changed (read-only). But I’m not sure if that’s it. Or if there’s another alternative.

Below is part of the code that generates the file, and the image of the final result. I tried to leave as much information as possible to help.

Version of Csvhelper: 27.1.1

Class:

public class DadosAnexo
{
        public string Matricula { get; set; }
        public string IdStc { get; set; }
        public string IdSip { get; set; }
        public string Nome { get; set; }
        public string Status { get; set; }
}

Method that generates the file:

public void GerarArquivo()
    { 
        var nomePasta = "123456"; 
        var nomeArquivo = "Arquivo_123456.csv";
        var caminhoArquivo = @"C:\" + nomePasta; 
        
        if (!Directory.Exists(caminhoArquivo))
            Directory.CreateDirectory(caminhoArquivo);

        using (var streamWriter = new StreamWriter(Path.Combine(caminhoArquivo, nomeArquivo)))
        using (var csvWriter = new CsvWriter(streamWriter, CultureInfo.InvariantCulture))
        {
            csvWriter.Context.RegisterClassMap<DadosAnexoMap>(); // mapeamento das coluna
            //csvWriter.Configuration.Delimiter = ";"; // aqui se rodar assim acontece um erro, pois essa propriedade não pode receber valor
            csvWriter.WriteRecords(GerarDadosAnexo());
            streamWriter.Flush();
        }

    }

Method containing the data:

public List<DadosAnexo> GerarDadosAnexo()
    {
        return new List<DadosAnexo>
        {
            new DadosAnexo
            {
                Matricula = "BC123456",
                IdStc = "Z123456",
                IdSip = "123456",
                Nome = "Marcos",
                Status = "ATIVO",
                CodOrigem = "123"
            },
            new DadosAnexo
            {
                Matricula = "BC123456",
                IdStc = "Z123456",
                IdSip = "123456",
                Nome = "Pedro",
                Status = "ATIVO",
                CodOrigem = "123"
            },
            new DadosAnexo
            {
                Matricula = "BC123456",
                IdStc = "Z123456",
                IdSip = "123456",
                Nome = "Amanda",
                Status = "ATIVO",
                CodOrigem = "123"
            }
        };
    }

Mapping class:

public class DadosAnexoMap : ClassMap<DadosAnexo>
{
    public DadosAnexoMap()
    {
         Map(m => m.Matricula).Name("MATRICULA");
         Map(m => m.IdStc).Name("ID STC");
         Map(m => m.IdSip).Name("ID SIP");
         Map(m => m.Nome).Name("NOME");
        Map(m => m.Status).Name("STATUS");
    }
}

Result (csv file opened by Excel), note that the data did not separate: inserir a descrição da imagem aqui

  • 1

    Actually the file is being generated correctly, excel that is not opening as a spreadsheet separated by comma. Already in Google Sheet opened right...

  • Have you checked if you are using the most current version of the library? The identification of , or of ; as list separator is also a configuration present in the language settings configured on the machine.

  • @novic, by direct Excel did not open as needed.

  • @Leandroangelo, I am using the latest version of the library (27.1.1). And the language of the machine is the Portuguese standard ABNT2.

  • It doesn’t even open! I reported the problem to you, but, it is not the file that is in trouble is excel that works like this...

1 answer

0


I found the solution. It was about the region when it stantiated the class Csvwriter. Where it is necessary to inform as a parameter. And Cultureinfo pass the language "en-BR" as parameter.

Where instead of being like this:

var csvWriter = new CsvWriter(streamWriter, CultureInfo.InvariantCulture);

Stay like this:

var csvWriter = new CsvWriter(streamWriter, new CultureInfo("pt-BR", true));

Thus when writing csv, it separates by ";" which is the standard of Excel when reading a csv file in the language en.

Browser other questions tagged

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