I have a query from SQL , I show it in a GridView in a web page ( ASP.NET ) and from there I export it to a flat file delimited with C # and I have problems with accents and special characters.
For example, the word MUÑOZ
, that the Ñ
must occupy a space, I get MUÑOZ
and it overflows the delimitation of the file.
protected void btnGenerar_Click(object sender, EventArgs e)
{
string nombre = "Archivo" + DateTime.Now.ToShortDateString();
//Encoding encoding = Encoding.UTF8;
Response.Clear();
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename="+nombre+".txt");
Response.Charset = "";
Response.ContentType = "application/text";
gvDatos.AllowPaging = false;
gvDatos.DataBind();
StringBuilder sb = new StringBuilder();
//añadimos la cabecera desde el texbox
string cab = txtCabecera.Text;
sb.Append(cab);
//append new line
sb.Append("\r\n");
CargarDatos();
for (int i = 0; i < gvDatos.Rows.Count; i++)
{
for (int k = 0; k < gvDatos.Columns.Count; k++)
{
//add separator
sb.Append(gvDatos.Rows[i].Cells[k].Text);
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}