简体   繁体   English

CSV文件导出存在编码问题

[英]CSV file export with encoding issues

I'm trying to export a SQL Server table into a CSV file: 我正在尝试将SQL Server表导出到CSV文件中:

Data in the table is like = Presentación or Difusión 表中的数据类似于=Presentación或Difusión

All is working good... but in the .CSV file I find: 一切正常...但是在.CSV文件中,我发现:

Presentación or Difusión

This is my .cs code: 这是我的.cs代码:

GridView1.AllowPaging = false;
GridView1.DataBind();

StringBuilder sb = new StringBuilder();

for (int k = 0; k < GridView1.Columns.Count; k++)
{
    // add separator
    sb.Append(GridView1.Columns[k].HeaderText + ';');
}

// append new line
sb.Append("\r\n");

for (int i = 0; i < GridView1.Rows.Count; i++)
{
    for (int k = 0; k < GridView1.Columns.Count; k++)
    {
        // add separator
        sb.Append(GridView1.Rows[i].Cells[k].Text + ';');
    }

    // append new line
    sb.Append("\r\n");
}

Encoding encoding = Encoding.UTF8;

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=OrigenDatos.csv");  
Response.Charset = encoding.EncodingName;
Response.ContentType = "application/text";
Response.ContentEncoding = Encoding.Unicode;
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();

Is UTF-8 the true charset? UTF-8是真正的字符集吗?

In order to correctly decode an HTML file, your browser needs to know which encoding to use. 为了正确解码HTML文件,您的浏览器需要知道使用哪种编码。 You can tell it to him by setting the charset parameter or by setting meta tag : <meta charset="UTF-8"> 您可以通过设置charset参数或通过设置meta标签来告诉他: <meta charset="UTF-8">

So: you need to save the HTML file using UTF-8 and declare that encoding properly. 因此:您需要使用UTF-8保存HTML文件并正确声明该编码。

It can also theoretically be that on your server default encoding is other as UTF-8 - then you should delete the AddDefaultCharset your_encoding from the .htaccess server file and write AddDefaultCharset utf-8 . 从理论上讲,它也可以是服务器上的默认编码为UTF-8以外的其他编码-然后应从.htaccess服务器文件中删除AddDefaultCharset your_encoding并编写AddDefaultCharset utf-8 But I am not sure that this is about your case. 但是我不确定这与您的情况有关。

EDIT: 编辑:

The byte &#243; 字节&#243; stands for Spanish character ó in UTF-8. 在UTF-8中代表西班牙语字符ó。 So I think you should not try to encode with other encodings, because it is definitely utf-8. 因此,我认为您不应该尝试使用其他编码进行编码,因为它绝对是utf-8。

You can try out this below code 您可以尝试以下代码

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, Encoding.UTF8) + "");
        Response.Cookies.Add(new System.Web.HttpCookie("fileDownload", "true"));
        Response.Charset = "";
        Response.ContentType = "application/csv";
        Response.ContentEncoding = Encoding.UTF8;
        Response.BinaryWrite(Encoding.UTF8.GetPreamble());
        Response.Write(outPutStringData); // Information which you want in .csv file
        Response.Flush();
        Response.End();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM