In sometine, we need to import, process and upload CSV file in our application. To process the CSV file, we have to write lot of custom code. To reduce the custom code to process the CSV, there is a fantastic helper class named 'CSVHelper'. It maps the data to a list of your custom class based on field attribute provided the helper class.  

Here is a simple example.

public class MyCustomClass
        [CsvField(Name = "Code")]
        public string Property1 { get; set; }

        [CsvField(Name = "Name")]
        public string Property2 { get; set; }

        [CsvField(Name = "Quantity")]
        public string Property3 { get; set; }
You can use Field Index or FIeld Name attribute.
Get the CSV data in the following way-
var streamReader = // Create a reader to your CSV file.
var csvReader = new CsvReader( streamReader );
List<MyCustomClass> myData = csvReader.GetRecords<MyCustomClass>();
Now you can use myDat list as you want. Have you seen how much smart  the code is?

It is available in NuGet package. Just search in NuGet package and reference to your project to work with this. 

Add new comment