Reading CSV files using C#

后端 未结 12 2507
野的像风
野的像风 2020-11-21 22:49

I\'m writing a simple import application and need to read a CSV file, show result in a DataGrid and show corrupted lines of the CSV file in another grid. For ex

12条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-21 23:30

    Here's a solution I coded up today for a situation where I needed to parse a CSV without relying on external libraries. I haven't tested performance for large files since it wasn't relevant to my particular use case but I'd expect it to perform reasonably well for most situations.

            static List> ParseCsv(string csv) {
                var parsedCsv = new List>();
                var row = new List();
                string field = "";
                bool inQuotedField = false;
    
                for (int i = 0; i < csv.Length; i++) {
                    char current = csv[i];
                    char next = i == csv.Length - 1 ? ' ' : csv[i + 1];
    
                    // if current character is not a quote or comma or carriage return or newline (or not a quote and currently in an a quoted field), just add the character to the current field text
                    if ((current != '"' && current != ',' && current != '\r' && current != '\n') || (current != '"' && inQuotedField)) {
                        field += current;
                    } else if (current == ' ' || current == '\t') {
                        continue; // ignore whitespace outside a quoted field
                    } else if (current == '"') {
                        if (inQuotedField && next == '"') { // quote is escaping a quote within a quoted field
                            i++; // skip escaping quote
                            field += current;
                        } else if (inQuotedField) { // quote signifies the end of a quoted field
                            row.Add(field);
                            if (next == ',') {
                                i++; // skip the comma separator since we've already found the end of the field
                            }
                            field = "";
                            inQuotedField = false;
                        } else { // quote signifies the beginning of a quoted field
                            inQuotedField = true; 
                        }
                    } else if (current == ',') { //
                        row.Add(field);
                        field = "";
                    } else if (current == '\n') {
                        row.Add(field);
                        parsedCsv.Add(new List(row));
                        field = "";
                        row.Clear();
                    }
                }
    
                return parsedCsv;
            }
    
    

提交回复
热议问题