C# 导出CSV文件时出现编码问题
我正在使用Asp.net mvc生成CSV文件,但葡萄牙语中的特殊字符有问题。我使用以下代码返回文件:C# 导出CSV文件时出现编码问题,c#,excel,asp.net-mvc,csv,encoding,C#,Excel,Asp.net Mvc,Csv,Encoding,我正在使用Asp.net mvc生成CSV文件,但葡萄牙语中的特殊字符有问题。我使用以下代码返回文件: public FileContentResult RelMatriculas(RelRematriculaVM model) { string fileContent = GenerateTheFile(); Response.Charset = "utf-8"; Response.ContentEncoding = Encoding.UTF8; return
public FileContentResult RelMatriculas(RelRematriculaVM model)
{
string fileContent = GenerateTheFile();
Response.Charset = "utf-8";
Response.ContentEncoding = Encoding.UTF8;
return File(new UTF8Encoding().GetBytes(fileContent), "text/csv", "RelMatriculas.csv");
}
我将utf8设置为编码,但是当保存文件并尝试在Excel中打开它时,我会看到垃圾字符而不是特殊字符
如果我只是在记事本中打开文件并保存它,然后在excel中再次打开它,就是正确显示字符。是否缺少将文件输出为UTF8的内容?问题是Excel需要BOM。发件人:
对BOM使用编码可能会有所帮助:
新的UTF8编码(true)
,或者只是编码。UTF8
var data = Encoding.UTF8.GetBytes(fileContent);
var result = Encoding.UTF8.GetPreamble().Concat(data).ToArray();
return File(result, "text/csv", "RelMatriculas.csv");