从字符串数组 C# 创建 CSV

Create CSV from String Array C#

我想创建一个 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] 具有逗号值时,它的格式错误。

改变

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

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

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

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