C# Export Datatable as CSV to Selected Folder (folderbrowserdialog)

  • Thread starter Thread starter psifreak
  • Start date Start date
P

psifreak

Guest
Ive got the generic function to copy a table to a csv file, but its only sending it to the bin/debug folder. I want to set this where I can date-stamp the file and have a folder browser popup to confirm where it gets sent to. I have a string setting Ive been trying to use, havent quite got it typed in right. File.WriteAllText(@Properties.Settings.Default.ExportFolder, sb.ToString()); Pretty sure its something to that effect, but I want the popup to aim at the setting that holds the file path initially. Unsure of A) if this should have a folderbrowserdialog or filebrowserdialog and B) how to make sure it goes to that folder.

public Export1(DataTable dt)
{

StringBuilder sb = new StringBuilder();

IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().
Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));

foreach (DataRow row in dt.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field =>
string.Concat("\"", field.ToString().Replace("\"", "\"\""), "\""));
sb.AppendLine(string.Join(",", fields));

}
File.WriteAllText("test.csv", sb.ToString());
}



May the fleas of a thousand camels feast happily on the lower regions of your enemies. And may their arms be too short to scratch!

Continue reading...
 
Back
Top