public void tablesave(DataGridView dataGridView1)
{
string path = "";
FolderBrowserDialog fdg = new FolderBrowserDialog();
fdg.Description = "请选择文件路径";
try
{
if (fdg.ShowDialog() == DialogResult.OK)
{
path = fdg.SelectedPath + "\\正算表格.csv";
}
else
{
throw new Exception();
}
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine(string.Join(",", dataGridView1.Columns.Cast<DataGridViewColumn>()
.Select(x => x.HeaderText)));
foreach (DataGridViewRow row in dataGridView1.Rows)
{
sw.WriteLine(string.Join(",", row.Cells.Cast<DataGridViewCell>()
.Select(x => x.Value)));
}
}
}
catch(Exception)
{
}
}
c#保存DataGridView中表格为csv(自用)
于 2024-05-31 16:26:26 首次发布