1 public class HomeController : Controller 2 { 3 static List<User> GetUsers() 4 { 5 List<User> list = new List<User>() { 6 new User{Id=1,Name="张三"}, 7 new User{Id=2,Name="lisi"}, 8 new User{Id=3,Name="wangwu"} 9 }; 10 return list; 11 } 12 public void SaveToExcel() 13 { 14 StringBuilder sb = new StringBuilder(); 15 sb.Append("id").Append(" "); 16 sb.Append("name").Append(" "); 17 List<User> list = GetUsers(); 18 foreach (var item in list) 19 { 20 sb.Append(item.Id).Append(" "); 21 sb.Append(item.Name).Append(" "); 22 } 23 Response.Clear(); 24 Response.Buffer = true; 25 Response.Charset = "UTF-8"; 26 Response.ContentEncoding = Encoding.UTF8; 27 Response.ContentType = "application/ms-excel"; 28 Response.AppendHeader("Content-Disposition", "attachment; filename=card.xls"); 29 Response.Write(sb.ToString()); 30 Response.End(); 31 }
这个该怎么设置下?谢谢
改成下面这样的:
Response.Charset = "GB2312";//定义输出字符集
Response.ContentEncoding = Encoding.Default;//输出内容的编码为默认编码
@大斌小呼: 那你检查一下你的Windows环境和Office,是不是英文版。我的是Win7英文版,但是在区域与语言设置里全部改成China/Chinese了。另外检查一下你的Excel,有个地方设置的,我的是Excel 2007
Response.Charset = "GB2312";试试。
刚试了下,也不行,中文的地方还是显示"
?????? |
"
@大斌小呼:
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.Default;
我这样就可以了。
Response.ContentEncoding = Encoding.UTF8;这个也得改
刚修改了下 ,这样修改也不行,还是乱码
建议用一些excel读写的组件去做。
GB2312
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.Default;
这个真能解决编码转换问题
public void SaveToExcel()
13 {
14 StringBuilder sb = new StringBuilder();
15 sb.Append("id").Append(" ");
16 sb.Append("name").Append("
");
17 List<User> list = GetUsers();
18 foreach (var item in list)
19 {
20 sb.Append(item.Id).Append(" ");
21 sb.Append(item.Name).Append("
");
22 }
23 Response.Clear();
24 Response.Buffer = true;
25 Response.Charset = "UTF-8";
26 Response.ContentEncoding = Encoding.UTF8;
27 Response.ContentType = "application/ms-excel";
28 Response.AppendHeader("Content-Disposition", "attachment; filename=card.xls");
29 //加上这句
30 Response.Write("<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=utf-8"/>" + sb.ToString());
31 Response.End();
32 }
给个赏呗!!
不太明白为什么加上那句话,就可以解决乱码的问题呢。
的确加上这句就解决了乱码
Response.Write("<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=utf-8"/>" + sb.ToString());
操蛋
我试了下,中文的还是乱码