0

I want to create a CSV but when a value has comma it gets formatted wrong.

List<string[]> output = new List<string[]>();
output.Add(valuesRow.ToArray());
int length = output.Count;
for (int index = 0; index < length; index++)
{
 stringBuilder.AppendLine(string.Join(delimiter,output[index]));
}

But when output[index] has value with comma its get formatted wrong.

2 Answers2

2

Change

stringBuilder.AppendLine(string.Join(delimiter,output[index]));

to

stringBuilder.AppendLine(string.Join(delimiter,output[index].Select(x=>"\"" + x + "\"")));
Eser
  • 12,031
  • 1
  • 19
  • 32
1

You can use CsvHelper to generate your .csv output. You can specify your columns and inject your value by field name.

https://joshclose.github.io/CsvHelper/

Babak Naffas
  • 11,988
  • 3
  • 35
  • 49