如何将非英语字符写入HttpResponse

时间:2012-03-14 03:18:49

标签: c# asp.net character-encoding httpresponse

我在写一些中文字符到http响应时遇到了问题。我将application / vnd.ms-excel设置为ContentType。

当我查看excel文件时,我看不到我写的字符。我也试过使用不同的编码,但它不起作用。我错过了什么吗?

 protected void Button1_Click(object sender, EventArgs e)
    {
        HttpResponse response = Page.Response;
        response.Clear();
        response.ClearHeaders();
        response.ContentEncoding = System.Text.UnicodeEncoding.GetEncoding("GB2312");
        response.Charset = "GB2312";
        response.AddHeader("Content-Disposition", "attachment;filename=myfile.xls");
        response.ContentType = "application/vnd.ms-excel;charset=GB2312";
        response.Write("盈亏 你好你好你怎么样");
        response.Flush();
        response.End();
    }

2 个答案:

答案 0 :(得分:1)

您是否尝试将其放入web.config

<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" responseHeaderEncoding="utf-8" />

然后输出为UTF8

答案 1 :(得分:1)

我们设法通过使用Response.BinaryWrite而不是Response.Write来解决它。多谢你们!