Problem with accentuation when exporting data from Sql Server to Excel

0

I have the following code inside a button responsible for performing the export of the data from my table to excel.

    string caminho = "c:\caminho";

        // criar um arquivo para escrever

        using (StreamWriter sw = File.CreateText(caminho))

        {
            string conn = @"Server=server;Database=base;Trusted_Connection=True;";

            SqlConnection cn = new SqlConnection(conn);               

            string sql = " Select * from CLIENTE";

            SqlCommand cmd = new SqlCommand(sql, cn);

            try

            {

                //abre a conexão e gera o datareader

                cn.Open();

                SqlDataReader dr = cmd.ExecuteReader();

                // percorre o datareader e escreve os dados no arquivo .xls definido

                while (dr.Read())

                {

                    sw.WriteLine(dr["NOME"].ToString() + "\t" + dr["ENDERECO"].ToString() + "\t" + dr["CEP"].ToString() + "\t" + dr["BAIRRO"].ToString() + "\t" + dr["CIDADE"].ToString() + "\t" + dr["UF"].ToString() + "\t" + dr["TELEFONE"].ToString());

                }

                //exibe mensagem ao usuario

                MessageBox.Show("Arquivo " + caminho + " gerado com sucesso.");

            }

            catch (Exception excpt)

            {

                MessageBox.Show(excpt.Message);

            }

        } 

The data is being exported normally, the problem is that the words containing special characters are coming unconfigured, I believe it is some configuration of Encoding but I do not know where to do it. Can anyone help me?

    
asked by anonymous 03.04.2018 / 16:36

1 answer

0

I believe this can help you.

In your App.Config (in case of being winforms), insert the following tag into the configuration:

<configuration> <system.web> <globalization uiCulture="pt-BR" culture="pt-BR" enableClientBasedCulture="true" /> </system.web> </configuration>

The link for a better understanding of this solution and more information about culture Info

    
03.04.2018 / 22:23