首页 > 解决方案 > 数据错误在 C# 中将 Gridview 导出为 .txt

问题描述

我一直在尝试从带有 sql 查询的 gridview 导出数据。我在gridview中有如下数据

id crédito   |  código |    Fecha                    |  Cliente                     |  Nro Identificación | Municipio   |   Valor   |   Saldo

78454        |  82025  |19/03/2011 12:00:00 a. m.    |  LÓPEZ LÓPEZ JAIRO ALBERTO   |   74322514         |    ARAUCA    |   1860000 |   620000

缺点是当您将它们导出为 .txt 时,您会以这种方式将它们扔掉 在此处输入图像描述

换句话说,波浪号不认识我,“ñ”也不认识。

到目前为止,我尝试过的是:

protected void ButtonGenerar_Click(object sender, EventArgs e)
        {
            string txt = string.Empty;

            foreach (TableCell rowtext in GridViewDatos.HeaderRow.Cells)
            {
                txt += rowtext.Text + ",";
            }

            txt += "\r\n";

            foreach (GridViewRow row in GridViewDatos.Rows)
            {
                foreach (TableCell datos in row.Cells)
                {
                    txt += datos.Text + ",";
                }
                txt += "\r\n";
            }

            Encoding encoding = Encoding.UTF8;
            var bytes = encoding.GetBytes(txt);
            MemoryStream stream = new MemoryStream(bytes);
            StreamReader reader = new StreamReader(stream);
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=namefile_" + DateTime.Now.ToShortDateString() + ".txt");
            Response.Charset = "";
            Response.ContentType = "application/text";
            Response.ContentEncoding = Encoding.Unicode;
            Response.Output.Write(reader.ReadToEnd());
            Response.Flush();
            Response.End();
        }

我该如何解决?

标签: c#

解决方案


推荐阅读