代码之家  ›  专栏  ›  技术社区  ›  Abdelrahman ELGAMAL

编码问题

  •  1
  • Abdelrahman ELGAMAL  · 技术社区  · 14 年前

    我想将数据从DaraGrid导出到Excel文件中。这些数据是法语的 这是我的方法:

    public static void ExportGridView( DataGrid dataGrid, string fileName)
        {
            HttpResponse m_Response = HttpContext.Current.Response;
            m_Response.Clear();
            m_Response.AddHeader("content-disposition",
                string.Format("attachment;filename={0}", fileName));
            m_Response.Charset = "";           
            m_Response.ContentType = "application/octet-stream"; //"application/vnd.xls";
            m_Response.ContentEncoding = Encoding.UTF32;
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            dataGrid.RenderControl(htmlWrite);
            m_Response.Write(stringWrite.ToString());
            m_Response.End();
    
    }
    

    否则输出的法语格式就不好

    (例如)

    英语=>英语

    兴业银行=>兴业银行

    3 回复  |  直到 14 年前
        1
  •  6
  •   Darin Dimitrov    14 年前

    尝试使用 ISO-8859-1 :

    public static void ExportGridView(DataGrid dataGrid, string fileName)
    {
        var response = HttpContext.Current.Response;
        response.Clear();
        response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
        var isoEncoding = Encoding.GetEncoding("iso-8859-1");
        response.Charset = isoEncoding.WebName;
        response.ContentType = "application/vnd.ms-excel";
        response.ContentEncoding = isoEncoding;
        using (var writer = new StringWriter())
        using (var htmlWriter = new HtmlTextWriter(writer))
        {
            dataGrid.RenderControl(htmlWriter);
            response.Write(writer.GetStringBuilder().ToString());
        }
        response.End();
    }
    

    另一种可能更好的方法是生成整个HTML页面并指定UTF8编码,IMHO更好:

    public static void ExportGridView(DataGrid dataGrid, string fileName)
    {
        var response = HttpContext.Current.Response;
        response.Clear();
        response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
        response.Charset = Encoding.UTF8.WebName;
        response.ContentType = "application/vnd.ms-excel";
        response.ContentEncoding = Encoding.UTF8;
        using (var writer = new StringWriter())
        using (var htmlWriter = new HtmlTextWriter(writer))
        {
            dataGrid.RenderControl(htmlWriter);
            string html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Test</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body>{0}</body></html>";
            response.Write(string.Format(html, writer.GetStringBuilder()));
        }
        response.End();
    }
    
        2
  •  0
  •   Dick Lampard    14 年前

        3
  •  0
  •   Gray    14 年前

    我怀疑他/她使用utf32是有原因的。我很想把这条线完全划掉,看看会发生什么。如果utf8是从Excel到DataGrid的,那么它就可以工作了,我想你是对的,但是它是相反的。这条线甚至可能不是问题。

    我更怀疑那些“StringWriter”对象。我不熟悉这些。如果您使用html来移动东西,浏览器不应该能够识别utf32,而是将其视为utf16,这可能是您的问题。