1
Reply

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

NJ  Bhanushali

NJ Bhanushali

Jul 14 2015 4:19 AM
573

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)