怎么解决这个乱码?

chenjing1104 2009-07-23 11:50:43
我写了一个导出,导出的名字是中文字,如果写个英文字就不是乱码,代码如下:

public void Bind()
{
string sql = "select * from userInfo";
DataTable dt = SQLServerDAL.DbHelperSQL.Query(sql).Tables[0];
StringWriter sw = new StringWriter();
sw.WriteLine("编号\t姓名\t年龄");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr["id"] + "\t" + dr["name"] + "\t" + dr["age"]);
}
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment; filename=报表.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();

}
请问各位前辈要怎么解决呀!
...全文
105 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongyin163 2009-07-23
  • 打赏
  • 举报
回复
一般都是编码不统一出现乱码,
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment; filename=报表.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
这里你设置两次编码,最终输出的gb2312,

看下浏览器里用的什么编码,改成gb2312可能就不乱了,或者用utf-8试试
Aderlee 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 chenjing1104 的回复:]
引用 7 楼 fengying0529 的回复:
用escape unescape 编解码


麻烦你能说具体一点吗!
[/Quote]

HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString();

或者GB2312
ff1222 2009-07-23
  • 打赏
  • 举报
回复
学习
chenjing1104 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fengying0529 的回复:]
用escape unescape 编解码
[/Quote]

麻烦你能说具体一点吗!
chenjing1104 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 aderlee 的回复:]
帮你改了一下,没测试的。

public void Bind()
    {
        string sql = "select * from userInfo";
        DataTable dt = SQLServerDAL.DbHelperSQL.Query(sql).Tables[0];
        StringWriter sw = new StringWriter();
        sw.WriteLine("编号\t姓名\t年龄");
        foreach(DataRow dr in dt.Rows)
        {
            sw.WriteLine(dr["id"] + "\t" + dr["name"] + "\t" + dr["age"]);
        }
Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename="+UrlPathEncode("报表.xls"));
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");
        Response.ContentType = "application/ms-excel";
        Response.Write(sw);
        Response.End();

    }
[/Quote]

还是不行呢!
fengying0529 2009-07-23
  • 打赏
  • 举报
回复
用escape unescape 编解码
chenjing1104 2009-07-23
  • 打赏
  • 举报
回复
好的、我试一下!
Aderlee 2009-07-23
  • 打赏
  • 举报
回复
帮你改了一下,没测试的。

public void Bind()
{
string sql = "select * from userInfo";
DataTable dt = SQLServerDAL.DbHelperSQL.Query(sql).Tables[0];
StringWriter sw = new StringWriter();
sw.WriteLine("编号\t姓名\t年龄");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr["id"] + "\t" + dr["name"] + "\t" + dr["age"]);
}
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename="+UrlPathEncode("报表.xls"));
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");
Response.ContentType = "application/ms-excel";
Response.Write(sw);
Response.End();

}
chenjing1104 2009-07-23
  • 打赏
  • 举报
回复
好像不行呢!
chenjing1104 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 aderlee 的回复:]
utf7,你的代码里,写一个就可以了。
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
[/Quote]
就在代码里加一名utf7,就可以了吗?
Aderlee 2009-07-23
  • 打赏
  • 举报
回复
utf7,你的代码里,写一个就可以了。
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
chenjing1104 2009-07-23
  • 打赏
  • 举报
回复
就是上面是用红色标识的地方,写英文导出的时候就不是乱码,要是写个中文导出的时候就是乱码
chenjing1104 2009-07-23
  • 打赏
  • 举报
回复
问题我自己已经解决了,不过还是要谢谢大家对我的帮助!
chenjing1104 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 aderlee 的回复:]
引用 9 楼 chenjing1104 的回复:
引用 7 楼 fengying0529 的回复:
用escape unescape 编解码


麻烦你能说具体一点吗!


HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString();

或者GB2312
[/Quote]

你说的这种我试了的,不行

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧