在C#ASP.net中,有人可以告诉我如何将数组/列表中的条目写入服务器上的CSV文件,然后打开文件吗?我认为第二部分将是 - Response.Redirect(" http://myserver.com/file.csv "),但不确定如何在服务器上写入文件.
此外,如果许多用户访问此页面,是否每次生成新的CSV文件或覆盖同一文件更好?如果两个用户都尝试访问相同的CSV文件等,是否会出现读/写/锁定问题?
更新:
这可能是一个愚蠢的问题,我在Google上搜索过,但我无法找到明确的答案 - 你如何将CSV文件写入网络服务器并将其导出到C#ASP.net?我知道如何生成它,但我想将其保存到www.mysite.com/my.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) 如果有办法从DataTable或DataSet生成CSV文件,请告诉我?具体而言,无需手动迭代DataTable行和连接.
请帮忙