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?