努力让我的 CSV 导出以显示“保存文件”对话框。它可以保存到文件中,但不允许用户保存。任何人都可以看到我的代码可能出错的地方吗?
string filename = Server.MapPath("~/download.csv");
StreamWriter sw = new StreamWriter(filename, false);
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
Response.Clear();
Response.ContentType = "application/csv";
Response.AddHeader("Content-Disposition", "attachment; filename=download.csv");
Response.WriteFile(filename);
Response.Flush();
Response.End();
我认为是弹出对话框的内容处置行,但也许我还需要做其他事情。
谢谢