写文件,乱码问题

C#写文件时,出现乱码的处理。

只设置charset="utf-8"有时还是不够的,有的“ö、Å”还是有问题的,这时加上下面代码的第11、12行,就可以解决乱码的问题了。

 1             HttpContext context = HttpContext.Current;
2 context.Response.Clear();
3 context.Response.ClearHeaders();
4 context.Response.ContentEncoding = Encoding.UTF8;
5 context.Response.Charset = "utf-8";
6
7 context.Response.ContentType = "application/ms-excel";
8 context.Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName + ".csv");
9
10 StreamWriter wtr = new StreamWriter(new MemoryStream(), Encoding.UTF8);

11 byte[] b = new byte[] { 0xEF, 0xBB, 0xBF };
12 context.Response.BinaryWrite( b);
原文地址:https://www.cnblogs.com/yahb/p/2159533.html