我正在尝试将数据从gridview导出到.csv并使用“保存文件”对话框进行保存,但是当我按下按钮时,什么也没发生。 Gridview可以正常工作,下面是代码:
public struct DataParameters
{
public List<Currency> currency;
public string Filename;
}
public static DataParameters dataParameters;
单击按钮-打开文件对话框,将文件导出到csv并保存
protected void Button1_Click(object sender, EventArgs e)
{
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "CSV
files (*.csv)|*.csv|All files (*.*)|*.*" ,
ValidateNames = true, InitialDirectory = @"C:\",
RestoreDirectory = true, CheckFileExists = true, CheckPathExists
= true, DefaultExt = "csv" }) {
if (sfd.ShowDialog() == DialogResult.OK)
{
dataParameters.currency = GridView1.DataSource as
List<Currency>;
dataParameters.Filename = sfd.FileName;
}
}
List<Currency> currencies = dataParameters.currency;
string filename = dataParameters.Filename;
using (StreamWriter sw = new StreamWriter(new FileStream(filename,
FileMode.Create), Encoding.UTF8))
{
StringBuilder sb = new StringBuilder();
foreach (Currency c in currencies)
{
sb.AppendLine(string.Format($"{c.Drzava} {c.Sifra_valute} {
{c.Valuta} {c.Jedinica} {c.Kupovni_tecaj} {c.Srednji_tecaj}
{c.Prodajni_tecaj}"));
}
sw.Write(sb.ToString());
}
}
当我按下按钮时什么也没发生