您好我已经搜索过创建csv文件的代码,但这只能通过指示默认位置来实现。如果我想定义文件位置,我该怎么办?
private void btnExport_Click(object sender, EventArgs e)
{
//Build the CSV file data as a Comma separated string.
string csv = string.Empty;
//Add the Header row for CSV file.
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
csv += column.HeaderText + ',';
}
//Add new line.
csv += "\r\n";
//Adding the Rows
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
//Add the Data rows.
csv += cell.Value.ToString().Replace(",", ";") + ',';
}
//Add new line.
csv += "\r\n";
}
//Exporting to CSV.
string folderPath = "C:\\CSV\\";
File.WriteAllText(folderPath + "DataGridViewExport.csv", csv);
}
答案 0 :(得分:4)
SaveFileDialog dialog = new SaveFileDialog();
DialogResult result = dialog.ShowDialog();
string selectedPath = "";
if (result == DialogResult.OK)
{
selectedPath = dialog.FileName;
}
你可以选择开始目录等选项,但总的来说它很容易使用。