Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

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

$
0
0
I've got the generic function to copy a table to a csv file, but it's 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 I've been trying to use, haven't quite got it typed in right.  File.WriteAllText(@Properties.Settings.Default.ExportFolder, sb.ToString());  Pretty sure it's 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!


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>