Importing CSV data into C# classes

前端 未结 7 1367
广开言路
广开言路 2020-12-02 01:45

I know how to read and display a line of a .csv file. Now I would like to parse that file, store its contents in arrays, and use those arrays as values for some classes I cr

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 02:21

    For a resilient, fast, and low effort solution, you can use CsvHelper which handles a lot of code and edge cases and has pretty good documentation

    First, install the CsvHelper package on Nuget

    a) CSV with Headers

    If your csv has headers like this:

    sport,date,team 1,team 2,score 1,score 2
    basketball,2011/01/28,Rockets,Blazers,98,99
    baseball,2011/08/22,Yankees,Redsox,4,3
    

    You can add attributes to your class to map the field names to your class names like this:

    public class SportStats
    {
        [Name("sport")]
        public string Sport { get; set; }
        [Name("date")]
        public DateTime Date { get; set; }
        [Name("team 1")]
        public string TeamOne { get; set; }
        [Name("team 2")]
        public string TeamTwo { get; set; }
        [Name("score 1")]
        public int ScoreOne { get; set; }
        [Name("score 2")]
        public int ScoreTwo { get; set; }
    }
    

    And then invoke like this:

    List records;
    
    using (var reader = new StreamReader(@".\stats.csv"))
    using (var csv = new CsvReader(reader))
    {
        records = csv.GetRecords().ToList();
    }
    

    b) CSV without Headers

    If your csv doesn't have headers like this:

    basketball,2011/01/28,Rockets,Blazers,98,99
    baseball,2011/08/22,Yankees,Redsox,4,3
    

    You can add attributes to your class and map to the CSV ordinally by position like this:

    public class SportStats
    {
        [Index(0)]
        public string Sport { get; set; }
        [Index(1)]
        public DateTime Date { get; set; }
        [Index(2)]
        public string TeamOne { get; set; }
        [Index(3)]
        public string TeamTwo { get; set; }
        [Index(4)]
        public int ScoreOne { get; set; }
        [Index(5)]
        public int ScoreTwo { get; set; }
    }
    

    And then invoke like this:

    List records;
    
    using (var reader = new StreamReader(@".\stats.csv"))
    using (var csv = new CsvReader(reader))
    {
        csv.Configuration.HasHeaderRecord = false;
        records = csv.GetRecords().ToList();
    }
    

    Further Reading

    • Reading CSV file and storing values into an array (295

提交回复
热议问题