我正在导入csv文件,此csv文件中的列可以包含逗号。所以当我导入它时会增加列数。
3,Stage3,"Desc Stage,test,test2",55.98,98.76
这是行,我希望我的列为:
3,
Stage3,
"Desc Stage,test,test2",
55.98,
98.76
答案 0 :(得分:0)
试试这个
public static string[] splitCSV(string CSVLineWithQuotedTextWithCommas)
{
Regex regExToSeperate = new Regex("(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)", RegexOptions.Compiled);
List<string> result = new List<string>();
string curr = null;
foreach (Match match in regExToSeperate.Matches(CSVLineWithQuotedTextWithCommas))
{
curr = match.Value;
if (0 == curr.Length)
{
result.Add("");
}
result.Add(curr.TrimStart(','));
}
return result.ToArray<string>();
}