解决导出Excel乱码的几种方法

public void SaveToExcel()
{
StringBuilder sb = new StringBuilder();
sb.Append("id").Append("\t");
sb.Append("name").Append("\n");
List<User> list = GetUsers();
foreach (var item in list)
{
    sb.Append(item.Id).Append("\t");
    sb.Append(item.Name).Append("\n");
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "UTF-8";
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=card.xls");
 
//加上这句             
Response.Write("<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=utf-8\"/>" + sb.ToString()); 
Response.End();
}

 

再看看遇到乱码已成功解决了问题的伙伴们是怎么解决的

<span style="font-size:18px;">Run-1</span>
 
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.Default;

 

这个真能解决编码转换问题

 

<span style="font-size:18px;">Run-2</span>

的确加上这句就解决了乱码

Response.Write("<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=utf-8\"/>" + sb.ToString()); 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.duanlonglong.com/qdjy/568.html