【问题标题】:C# Unable to print euro symbol into a file (when opening with Excel)C# 无法将欧元符号打印到文件中(使用 Excel 打开时)
【发布时间】: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


【解决方案1】:

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

    return response;
}

采取你最喜欢的解决方案。

【讨论】:

  • 感谢使用 Windows-1252 编码为我省去了一些麻烦
  • 这个答案应该得到更多的支持。这是我对内容标题和编码进行一天研究后发现的唯一正确解释。谢谢!
  • Windows-1252 为我解决了这个问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 2010-12-30
  • 2020-09-23
  • 1970-01-01
  • 2023-03-21
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
相关资源
最近更新 更多