【发布时间】:2013-08-23 12:44:53
【问题描述】:
我在使用 get 方法进入 Web api 控制器时遇到问题。此方法返回一个 HttpResponseMessage 对象,该对象具有一个 HttpContent 和一个包含欧元符号的 csv 文件。当该方法返回文件时,不会打印欧元符号。 该方法的代码如下:
string export = ... //string with fields separed by ';' and with euro symbol
HttpResponseMessage response = new HttpResponseMessage();
UTF8Encoding encoding = new UTF8Encoding();
Byte[] buffer = encoding.GetBytes(export);
response.Content = new ByteArrayContent(buffer);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Export.csv" };
response.Content.Headers.ContentLength = export.Length;
response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddDays(1));
return response;
当我打开文件时,欧元符号显示不正确。 你能给我一个答案吗?
非常感谢。
【问题讨论】:
-
“没有正确显示”是什么意思?它在那里吗?也许您打开的文件编码错误?
-
通过设置
response.ContentEncoding = System.Text.Encoding.Utf8; response.Charset = encoding.BodyName;试试 -
我看到的是 '€' 而不是 '€'。我正在用 Excel 打开文件。
-
response.Content.Headers.ContentEncoding 是只读属性
-
我注意到如果我用记事本打开文件,欧元符号会正确显示。问题是我想用Excel打开文件,有没有办法设置Excel单元格编码?
标签: c# excel utf-8 httpcontent