以下代码可以正常工作,并将结果导出到D驱动器中提到的文件中。我们如何更改它以便打开POPUP并询问将文件保存在何处
受保护的无效Button1_Click(对象发送者,EventArgs e) {
using (StreamWriter writer = new StreamWriter("D:\\dump.csv"))
{
WriteDataTable(dt, writer, true);
}
}
public static void WriteDataTable(DataTable sourceTable, TextWriter writer, bool includeHeaders)
{
if (includeHeaders)
{
IEnumerable<String> headerValues = sourceTable.Columns
.OfType<DataColumn>()
.Select(column => QuoteValue(column.ColumnName));
writer.WriteLine(String.Join(",", headerValues));
}
IEnumerable<String> items = null;
foreach (DataRow row in sourceTable.Rows)
{
items = row.ItemArray.Select(o => QuoteValue(o?.ToString() ?? String.Empty));
writer.WriteLine(String.Join(",", items));
}
writer.Flush();
}
private static string QuoteValue(string value)
{
return String.Concat("\"",
value.Replace("\"", "\"\""), "\"");
}