Kei*_*ith 6 c# csv asp.net export-to-csv
我正在尝试将数据导出到csv文件,因为数据中有中文字符我必须使用unicode ..但是在添加unicode的前导码后,逗号不会被识别为分隔符,所有数据现在都被写入第一栏.我不确定是什么问题.下面是我在.ashx文件中编写的代码.
DataView priceQuery = (DataView)context.Session["priceQuery"];
String fundName = priceQuery.Table.Rows[0][0].ToString().Trim().Replace(' ', '_');
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.ContentType = "text/csv";
context.Response.ContentEncoding = System.Text.Encoding.Unicode;
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fundName + ".csv");
context.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
String output = fundName + "\n";
output += "Price, Date" + "\n";
foreach (DataRow row in priceQuery.Table.Rows)
{
string price = row[2].ToString();
string date = ((DateTime)row[1]).ToString("dd-MMM-yy");
output += price + "," + date + "\n";
}
context.Response.Write(output);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8208 次 |
| 最近记录: |