NJ  Bhanushali

NJ Bhanushali

  • NA
  • 13
  • 15.7k

How to change separator to write csv file using c#?

Jul 14 2015 4:19 AM

I have created CSV file using comma (,) separator using following code.

DataTable dt = dataSet.Tables[0]; 
StringBuilder csv = new StringBuilder();
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int k = 0; k < dt.Columns.Count; k++)
{
csv.Append(dt.Rows[i][k].ToString() + ',');
}
csv.Append("\r\n");
}
File.WriteAllText(filePath, csv.ToString());

If I use comma, data divided into column properly. But when I try "|", it doesn't divide into columns
How can I change separator? I want to use "|" as a separator.


Answers (1)