Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting from a dataset to a tab delimited file

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

like image 441
JOE SKEET Avatar asked Dec 16 '10 20:12

JOE SKEET


2 Answers

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();
like image 109
KeithS Avatar answered Nov 15 '22 23:11

KeithS


Same problem here. Got a better one for you:

var builder = new StringBuilder();
foreach (DataRow row in dataSet.Tables.First().Rows) {
   builder.AppendLine(String.Join("\t", row.ItemArray));
}
File.WriteAllText(filePath, builder.ToString());

Its a bit more compact and (possibly) a tad faster.

like image 31
Steven de Salas Avatar answered Nov 16 '22 01:11

Steven de Salas