asp.net mvc C# 导出csv 乱码解决方法
时间:2011-01-03 来源:pic_name
用 notepad打开,发现文件中的内容没有乱码,看编码是uft-8无BOM;
手动转为有BOM的uft-8后,用excel打开就正常了;
所以要导出的文件正常打开,就要导入有BOM的文件;
有BOM以0xEF;0xBB;0xBF; 开头;C#
byte[] buffer = System.Text.Encoding.GetEncoding("utf-8").GetBytes(str); Response.AppendHeader("Content-Disposition", "attachment;filename=\"截止" + totolMonth.EndTime.ToShortDateString() + "结算报表.csv\""); Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); byte[] outBuffer = new byte[buffer.Length + 3]; outBuffer[0] = (byte)0xEF;//有BOM,解决乱码 outBuffer[1] = (byte)0xBB; outBuffer[2] = (byte)0xBF; Array.Copy(buffer, 0, outBuffer, 3, buffer.Length); return File(outBuffer, "application/ms-excel");
不过本方法只能在win里用吧,其它操作系统就不知道了。
相关阅读 更多 +