File generation with invalid character

Asked

Viewed 47 times

0

I developed the code below to put information of a return from a bank select in a CSV file, however as you can see in attached print, some characters are being sent wrong even if the return from select is correct, in which way I could put a UTF-8 or other charset. One interesting thing that also did not understand what happened is that big numbers are coming wrong in the display but when I click on them appears the number correctly.

p.s the commented part was something I tried to use but did not succeed.

inserir a descrição da imagem aqui

                    List<RemessaArquivo> Dados = bll.Dados(ID);

                    System.Text.StringBuilder ConteudoArq = new System.Text.StringBuilder();
                    System.Text.StringBuilder ConteudoLin = new System.Text.StringBuilder();

                    //Matriz para criar todas as linhas e colunas com os dados
                    Dados.ForEach(x =>
                    {
                        // uso do reflection nas linhas
                        Type type = typeof(RemessaArquivo);
                        PropertyInfo[] properties = type.GetProperties();
                        foreach (PropertyInfo property in properties)
                        {
                            ConteudoLinha.Append(property.GetValue(x, null) + ";");

                        }
                        ConteudoArquivo.AppendLine(ConteudoLin.ToString());
                        ConteudoLinha.Clear();
                    });
                    /* string auxConversao = "";
                     byte[] bytes = Encoding.Default.GetBytes(ConteudoArquivo.ToString());
                     auxConversao = Encoding.UTF8.GetString(bytes);
                     */

                    //Definindo onde o arquivo será gerado
                    using (System.IO.StreamWriter arquivoCSV = new StreamWriter(@"C:\ArquivosRemessa\arquivo.csv", false))
                    {
                        arquivoCSV.WriteLine(ConteudoArq.ToString());
                        arquivoCSV.Close();
                    }

1 answer

0


Good afternoon, Matheus

Have you tried setting the encoding you want? in the line below:

   using (System.IO.StreamWriter arquivoCSV = new StreamWriter(@"C:\ArquivosRemessa\arquivo.csv", false)) 

Utilize:

   using (System.IO.StreamWriter arquivoCSV = new StreamWriter(@"C:\ArquivosRemessa\arquivo.csv", false, System.Text.Encoding.UTF8)) 

Or even System.Text.Encoding.Default

I hope it helped you.

Att

  • I have tried yes, I have the following error return:

  • Error 57 Argument 2: cannot Convert from 'System.Text.Encoding' to 'bool'

  • 1

    Based on what you told me, I thought to send the false between the 2 parameters, I edited your question, thank you very much, the data are coming correctly!

Browser other questions tagged

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