【问题标题】:Importing csv file with commas in it Asp.net [duplicate]在 Asp.net 中导入带有逗号的 csv 文件 [重复]
【发布时间】:2016-08-12 07:36:02
【问题描述】:

我正在导入一个 csv 文件,并且此 csv 文件中的列可以包含逗号。因此,当我导入它时,它会增加列。

3,Stage3,"Desc Stage,test,test2",55.98,98.76

这是行,我希望我的列是:

3,
Stage3,
"Desc Stage,test,test2",
55.98,
98.76

【问题讨论】:

  • 什么样的疯子选择用逗号分隔数据的.csv格式?

标签: c# asp.net csv google-sheets export-to-csv


【解决方案1】:

试试这个

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

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    相关资源
    最近更新 更多