How do I handle line breaks in a CSV file using C#?

前端 未结 14 1921
情书的邮戳
情书的邮戳 2020-12-15 08:43

I have an Excel spreadsheet being converted into a CSV file in C#, but am having a problem dealing with line breaks. For instance:

\"John\",\"23\",\"555-555         


        
14条回答
  •  臣服心动
    2020-12-15 09:21

    There is a built-in method for reading CSV files in .NET (requires Microsoft.VisualBasic assembly reference added):

    public static IEnumerable ReadSV(TextReader reader, params string[] separators)
    {
        var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader);
        parser.SetDelimiters(separators);
        while (!parser.EndOfData)
            yield return parser.ReadFields();
    }
    

    If you're dealing with really large files this CSV reader claims to be the fastest one you'll find: http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

提交回复
热议问题