Reading CSV files using C#

后端 未结 12 2488
野的像风
野的像风 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:23

    CSV can get complicated real fast.

    Use something robust and well-tested:
    FileHelpers: www.filehelpers.net

    The FileHelpers are a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.

    0 讨论(0)
  • 2020-11-21 23:26

    Another one to this list, Cinchoo ETL - an open source library to read and write CSV files

    For a sample CSV file below

    Id, Name
    1, Tom
    2, Mark
    

    Quickly you can load them using library as below

    using (var reader = new ChoCSVReader("test.csv").WithFirstLineHeader())
    {
       foreach (dynamic item in reader)
       {
          Console.WriteLine(item.Id);
          Console.WriteLine(item.Name);
       }
    }
    

    If you have POCO class matching the CSV file

    public class Employee
    {
       public int Id { get; set; }
       public string Name { get; set; }
    }
    

    You can use it to load the CSV file as below

    using (var reader = new ChoCSVReader<Employee>("test.csv").WithFirstLineHeader())
    {
       foreach (var item in reader)
       {
          Console.WriteLine(item.Id);
          Console.WriteLine(item.Name);
       }
    }
    

    Please check out articles at CodeProject on how to use it.

    Disclaimer: I'm the author of this library

    0 讨论(0)
  • 2020-11-21 23:29

    I'd highly suggest using CsvHelper.

    Here's a quick example:

    public class csvExampleClass
    {
        public string Id { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
    }
    
    var items = DeserializeCsvFile<List<csvExampleClass>>( csvText );
    
    public static List<T> DeserializeCsvFile<T>(string text)
    {
        CsvReader csv = new CsvReader( new StringReader( text ) );
        csv.Configuration.Delimiter = ",";
        csv.Configuration.HeaderValidated = null;
        csv.Configuration.MissingFieldFound = null;
        return (List<T>)csv.GetRecords<T>();
    }
    

    Full documentation can be found at: https://joshclose.github.io/CsvHelper

    0 讨论(0)
  • 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<List<string>> ParseCsv(string csv) {
                var parsedCsv = new List<List<string>>();
                var row = new List<string>();
                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<string>(row));
                        field = "";
                        row.Clear();
                    }
                }
    
                return parsedCsv;
            }
    
    
    0 讨论(0)
  • 2020-11-21 23:35

    Don't reinvent the wheel. Take advantage of what's already in .NET BCL.

    • add a reference to the Microsoft.VisualBasic (yes, it says VisualBasic but it works in C# just as well - remember that at the end it is all just IL)
    • use the Microsoft.VisualBasic.FileIO.TextFieldParser class to parse CSV file

    Here is the sample code:

    using (TextFieldParser parser = new TextFieldParser(@"c:\temp\test.csv"))
    {
        parser.TextFieldType = FieldType.Delimited;
        parser.SetDelimiters(",");
        while (!parser.EndOfData) 
        {
            //Processing row
            string[] fields = parser.ReadFields();
            foreach (string field in fields) 
            {
                //TODO: Process field
            }
        }
    }
    

    It works great for me in my C# projects.

    Here are some more links/informations:

    • MSDN: Read From Comma-Delimited Text Files in Visual Basic
    • MSDN: TextFieldParser Class
    0 讨论(0)
  • 2020-11-21 23:35

    First of all need to understand what is CSV and how to write it.

    1. Every next string ( /r/n ) is next "table" row.
    2. "Table" cells is separated by some delimiter symbol. Most often used symbols is \t or ,
    3. Every cell possibly can contain this delimiter symbol (cell must to start with quotes symbol and ends with this symbol in this case)
    4. Every cell possibly can contains /r/n sybols (cell must to start with quotes symbol and ends with this symbol in this case)

    The easiest way for C#/Visual Basic to work with CSV files is to use standard Microsoft.VisualBasic library. You just need to add needed reference, and the following string to your class:

    using Microsoft.VisualBasic.FileIO;
    

    Yes, you can use it in C#, don't worry. This library can read relatively big files and supports all of needed rules, so you will be able to work with all of CSV files.

    Some time ago I had wrote simple class for CSV read/write based on this library. Using this simple class you will be able to work with CSV like with 2 dimensions array. You can find my class by the following link: https://github.com/ukushu/DataExporter

    Simple example of using:

    Csv csv = new Csv("\t");//delimiter symbol
    
    csv.FileOpen("c:\\file1.csv");
    
    var row1Cell6Value = csv.Rows[0][5];
    
    csv.AddRow("asdf","asdffffff","5")
    
    csv.FileSave("c:\\file2.csv");
    
    0 讨论(0)
提交回复
热议问题