该方法的代码如下:
string export = ... //string with fIElds separed by ';' and with euro symbolhttpResponseMessage 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;
当我打开文件时,欧元符号无法正确显示.
你能给我一个答案吗?
非常感谢.
解决方法 如上所述,这在Excel中不起作用,因为€符号没有正确显示(尽管它在任何纯文本编辑器中).[httpPost("csv")]public httpResponseMessage GetCvsReport(){ var response = new httpResponseMessage(httpStatusCode.OK); var content = "12€;3;test"; var enCoding = EnCoding.UTF8; response.Content = new StringContent(content,enCoding,"text/csv"); response.Content.headers.Contentdisposition = new ContentdispositionheaderValue("attachment") { filename = yourfile.csv" }; return response;}
我发现以下解决方案似乎正常工作.
使用windows-1252编码
似乎通过使用Windows-1252编码Excel能够正确解释€符号.
[httpPost("csv")]public httpResponseMessage GetCvsReport(){ var response = new httpResponseMessage(httpStatusCode.OK); var content = "12€;3;test"; var enCoding = EnCoding.GetEnCoding("windows-1252"); response.Content = new StringContent(content,"text/csv"); ...}
前置BOM(字节顺序标记)
另一个有效的解决方案是附加正确的BOM,如下所示:
[httpPost("csv")]public httpResponseMessage GetCvsReport(){ var response = new httpResponseMessage(httpStatusCode.OK); var content = "12€;3;test"; var enCoding = EnCoding.UTF8; content = enCoding.GetString(new byte[] { 0xEF,0xBB,0xBF }) + content; response.Content = new StringContent(content,"text/csv"); response.Content.headers.Contentdisposition = new ContentdispositionheaderValue("attachment") { filename = yourfile.csv" }; return response;}
采取您最喜欢的解决方案.
总结以上是内存溢出为你收集整理的C#无法将欧元符号打印到文件中(使用Excel打开时)全部内容,希望文章能够帮你解决C#无法将欧元符号打印到文件中(使用Excel打开时)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)