Write ; in . CSV without separating the cell

Asked

Viewed 192 times

1

I have a C# project that is normally generating a file with extension. CSV, however I want to write a ;(point and comma) but it does is to separate the cell. There is some way to write without that happening?

Code:

arquivo.Detalhe = new List<Detalhe>();
int sequencial = 1;
foreach (var c in clientes)
{
   Detalhe d = new Detalhe();
   var cob = c.Value.First();
   d.Endereco = c.Key.Endereco + "\";\"";

   d.Bairro = c.Key.Bairro.Nome + "\";\"";

   d.Cidade = c.Key.NomeCidade + "\";\"";
   arquivo.DetalheGD7.Add(d);
}

Creating the archive:

var arquivo = Service.CriarArquivo(paraEnviar, inclusaoRadioButton.Checked, motivo);
TextFile.Serializer.Serialize(arquivo, localDoArquivoTextBox.Text + "\\ARQUIVO_.CSV");

The photo of how the file comes out:

inserir a descrição da imagem aqui

Ignore the spacing, it’s the other information that hasn’t been filled in yet.

I believe that there will be no need to show the other files, since my doubt is just how to write the ; without the break of the cell happens.

  • What do you mean by "separate/break the cell"? Where you are opening the generated file to query, in Excel?

  • 1

    @Pedrogaspar the ; can be interpreted as cell splitter in spreadsheet readers like excel and Calc. Please add a row of your generated CSV file that contains a ;, generally what is done is to leave the entire cell within ". It may be that this is just the setting of how your file is interpreted by these readers.

  • @Guilhermecostamilam I tried to put in all forms, but still could not put. I tried to configure excel to not understand ; as a cell splitter but without success.

  • @Brunomiqueas put the text of your CSV, for example, RUA GOV SAMPAIO; CENTRO - FORTALEZA, FORTALEZA

  • @Guilhermecostamilam Do you speak of putting via code? I tried via code but it did not help. And if I edit by CSV, it is good. But is there no other solution? And that’s what you meant by "putting the text of your CSV"?

  • Have you ever opened a CSV file with a text editor? Like the notepad, Notepad++, ...? If not, do this and you will understand

  • O ; appears there this way: GOV SAMPAIO STREET; CENTER - FORTRESS; FORTRESS;. What should I do?

  • 1

    just write to the first line of your Sep file=_

  • 1

    "_" being your new separator, it can be anything, literally, e.g.=+

  • Thank you all, you solved my problem.

Show 5 more comments

1 answer

1


According to RFC 4180, item 2.6, a field containing line breaks, quotation marks and the field separator MUST be printed with quotation marks around it.

See: https://tools.ietf.org/html/rfc4180


Detail. You’re adding ";" address, not just the ;, i.e., its field contains the field separation character itself, which may be causing the problem instead of the semicolon.

In the same RFC, item 2.7: For a field to contain quotation marks, the quotation marks must appear folded.

"Endereço;";"Bairro;";Cidade
"Endereço"";""";"Bairro"";""";Cidade

Is interpreted as:

Endereço; | Bairro; | Cidade
Endereço";" | Bairro";" | Cidade

It seems confusing at first, but notice that the ; are with different colors here, and that even the large amount of quotes keeps leaving the separators in black in the right places.

Browser other questions tagged

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