繁体   English   中英

如何对csv导出进行HTML编码

[英]How to HTML encode a csv export

我明白了

 Quattrode® 

从HTML编码为的字符串

Quattrode® 

在Excel 2007中查看时。

常规

        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, ","));

基本上DataGridToCSV()使用HttpUtility.HtmlDecode(stringBuilder.ToString()); 我可以使用visual studio的文本可视化工具,看看字符串看起来是否正确(Quattrode®)。

因此, Response过程或Excel对文件的解释中的某些内容不正确。 知道怎么解决?


UPDATE
事实证明,这在Excel或写字板中无法正确解释。 我在记事本中打开它,符号显示正确。

我在这里找到答案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 =“”;

尝试使用像UTF-8这样的东西

暂无
暂无

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

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