我有一个用于将数据表值下载为 csv 格式的代码。它工作正常,我可以下载 csv。但问题是我的数据表在我下载 csv 时包含一些丹麦字符,csv 文件包含除丹麦字符之外的所有详细信息替换为其他一些字符。这是我的代码。
string myXMLfile = Server.MapPath("~/App_Data/myfilecsv.xml");
DataSet ds = new DataSet();
System.IO.FileStream fsReadXml = new System.IO.FileStream(myXMLfile,
System.IO.FileMode.Open);
try
{
ds.ReadXml(fsReadXml);
DataTable dt = ds.Tables[1];
System.Text.StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().
Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dt.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
foreach (var item in fields)
{
sb.AppendFormat("\"{0}\",", item.Replace("\"", "\"\""));
}
sb = sb.Remove(sb.Length - 1, 1);
sb.Append("\n");
}
string attachment = "attachment; filename=mycsvfile.csv";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/csv";
Response.Write(sb.ToString());
Response.End();
}
catch (Exception ex)
{
umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error, 00002, ex.ToString());
}
finally
{
fsReadXml.Close();
}
谁能给个解决办法???