C#无法将欧元符号打印到文件中(使用Excel打开时) [英] C# Unable to print euro symbol into a file (when opening with Excel)

查看:141
本文介绍了C#无法将欧元符号打印到文件中(使用Excel打开时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Web api控制器中的get方法有疑问.此方法返回一个HttpResponseMessage对象,该对象的HttpContent带有csv文件,其中包含欧元符号.当该方法返回文件时,不会打印欧元符号. 该方法的代码如下:

I have a problem with a get method into a web api controller. This method returns a HttpResponseMessage object which has a HttpContent with a csv file, which contains euro symbols. When the method returns the file, the euro symbol isn't printed. The code of the method is the following:

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;

当我打开文件时,欧元符号显示不正确. 你能给我一个答案吗?

When I open the file, the euro symbol doesn't appear correctly. Could you give me an answer?

非常感谢.

推荐答案

如上所述,由于€符号未正确显示(尽管在任何纯文本编辑器中),因此这在Excel中不起作用.

As mentioned, this doesn't work in Excel as the € sign isn't shown properly (although it is in any plain text editor).

[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;
}

我发现以下似乎正常工作的解决方案.

I found the following solutions that seem to work properly.

似乎通过使用 Windows-1252 编码,Excel能够正确解释€符号.

It seems that by using Windows-1252 encoding Excel is able to correctly interpret the € symbol.

[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, encoding , "text/csv");
    ...
}

添加BOM(字节顺序标记)

另一个可行的解决方案是像这样添加正确的BOM:

Prepend the BOM (Byte order mark)

Another solution that works is to append the correct BOM like this:

[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, encoding , "text/csv");
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = yourfile.csv"
    };

    return response;
}

采用您最喜欢的解决方案.

Take the solution you like most.

这篇关于C#无法将欧元符号打印到文件中(使用Excel打开时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆