Excel和CSV导出不会正确显示波斯字符

时间:2017-12-25 09:17:54

标签: c# asp.net export-to-excel export-to-csv persian

将SQL Server数据导出到.csv时,波斯语中的短语无法正确显示。

string date = Helper.ToPersianDate(DateTime.Now).Replace("/", "");
date += ".csv";

Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=" + date);
Response.ContentType = "text/csv";
Response.Charset = Encoding.Unicode.ToString();

foreach (DataRow r in ds.Tables[0].Rows)
{
    Response.Write(r[0]);
    Response.Write("\r\n");
}
Response.End();      

1 个答案:

答案 0 :(得分:0)

string date = Helper.ToPersianDate(DateTime.Now).Replace("/", "");
date += ".csv";

Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=" + date);
Response.ContentType = "text/csv";
Response.Charset = Encoding.Unicode.ToString();
Response.ContentEncoding = System.Text.Encoding.Unicode;

/*
 save utf-8 with BOM
 GetPreamble
 */  
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

foreach (DataRow r in ds.Tables[0].Rows)
{
    Response.Write(r[0]);
    Response.Write("\r\n");
}
Response.End();