Exporting from a dataset to a tab delimited file

JOE SKEET picture JOE SKEET · Dec 16, 2010 · Viewed 15.6k times · Source

I have data in a dataset. How would I export it into a tab delimited file?

Answer

KeithS picture KeithS · Dec 16, 2010
var builder = new StringBuilder()

foreach(var row in dataSet.Tables.First().Rows)
{
   foreach(var cell in row.ItemArray)
   {
      builder.Append(cell.ToString());
      if(cell != row.Cells.Last())
         builder.Append("\t");
   }
   builder.Append(Environment.NewLine);
}

var file = new FileStream(filePath);
var writer = new StreamWriter(file);
writer.Write(builder.ToString());
writer.Flush();
writer.Close();