我使用以下代码将数据导出为csv格式。通常将其存储到系统桌面。我想保存各种位置,这就是为什么需要保存对话框。但我不能这样做,请帮我做..
StringBuilder sb = new StringBuilder();
var columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName).ToArray();
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dt.Rows)
{ var fields = row.ItemArray.Select(field => field.ToString().Replace(","," "));
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +"/test.csv", sb.ToString());
答案 0 :(得分:0)
如果您需要打开“保存”对话框,则需要将内容处置标头添加到响应中:
Response.AddHeader("content-disposition", "attachment;filename=test.csv");
现在您可以将字符串写入响应:
Response.Write(sb.ToString());
Response.End(); //VERY IMPORTANT