简体   繁体   English

如何对csv导出进行HTML编码

[英]How to HTML encode a csv export

I get 我明白了

 Quattrode® 

From a string that is HTML encoded as 从HTML编码为的字符串

Quattrode® 

when viewing inside Excel 2007. 在Excel 2007中查看时。

Routine 常规

        Response.Clear();
        Response.Buffer = true;

        Response.ContentType = "text/comma-separated-values";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"Expired.csv\"");

        Response.RedirectLocation = "export.csv";

        Response.Charset = "";
        //Response.Charset = "UTF-8";//this does not work either.
        EnableViewState = false;

        ExportUtility util = new ExportUtility();

        Response.Write(util.DataGridToCSV(gridViewExport, ","));

Basically DataGridToCSV() uses HttpUtility.HtmlDecode(stringBuilder.ToString()); 基本上DataGridToCSV()使用HttpUtility.HtmlDecode(stringBuilder.ToString()); and I can use visual studio's text visualizer and see the string looks correct (Quattrode®). 我可以使用visual studio的文本可视化工具,看看字符串看起来是否正确(Quattrode®)。

So something in the Response procedure or in Excel's interpretation of the file is not correct. 因此, Response过程或Excel对文件的解释中的某些内容不正确。 Any idea how to fix? 知道怎么解决?


UPDATE UPDATE
Turns out that this is not interpreted properly in Excel or WordPad. 事实证明,这在Excel或写字板中无法正确解释。 I open it up in Notepad and the symbol shows up properly. 我在记事本中打开它,符号显示正确。

I found the answer here https://stackoverflow.com/a/3300854/150342 and here https://stackoverflow.com/a/6460488/150342 Then put together this code: 我在这里找到答案https://stackoverflow.com/a/3300854/150342并在这里https://stackoverflow.com/a/6460488/150342然后把这段代码放在一起:

    public static void WriteCSVToResponse(string csv, string filename)
    {
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.ClearHeaders();
        response.ClearContent();
        response.ContentType = "text/csv";
        response.AddHeader("content-disposition", "attachment; filename=" + filename);
        response.ContentEncoding = Encoding.UTF8;

        byte[] BOM = new byte[] { 0xef, 0xbb, 0xbf };
        response.BinaryWrite(BOM);//write the BOM first
        response.BinaryWrite(Encoding.UTF8.GetBytes(csv));
        response.Flush();
        response.End();
    }

也试试这个

Response.ContentEncoding = Encoding.UTF32;

Response.Charset = ""; Response.Charset =“”;

Try using something like UTF-8 尝试使用像UTF-8这样的东西

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

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