从String Array C#创建CSV

时间:2018-02-08 21:57:26

标签: c# export-to-csv comma

我想创建一个CSV,但是当一个值有逗号时,它的格式错误。

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]));
}

但是当output[index]的逗号值为逗号时,其格式错误。

2 个答案:

答案 0 :(得分:2)

更改

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

stringBuilder.AppendLine(string.Join(delimiter,output[index].Select(x=>"\"" + x + "\"")));

答案 1 :(得分:1)

您可以使用CsvHelper生成.csv输出。您可以指定列并按字段名称注入值。

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