← Back to articles

Automatic class generator for CsvHelper

CsvHelper is great, but writing the model and mapper classes is tedious. Paste your CSV header on this page and it generates both classes for you.

Tedd

CsvHelper helps in reading CSV files. But creating the model and mapper classes can be a bit time consuming. On this page you can generate the classes. Just fill inn the fields below with the header for your CSV file. It will generate the model and a mapper class.

Code sample to use classes generated:

public List<TModel> ReadCsvFile<TModel, TModelMapper>(string file) where TModelMapper : ClassMap<TModel>
{
 var configuration = new Configuration
 {
 IncludePrivateMembers = false,
 IgnoreBlankLines = true,
 Delimiter = ";" // You may want to change this
 };
 configuration.RegisterClassMap<TModelMapper>();
 using (var csvFile = File.OpenText(file))
 {
 var parser = new CsvHelper.CsvReader(csvFile, configuration);
 return parser.GetRecords<TModel>().ToList();
 }
}

// Then you read the file like this
var customers = ReadCsvFile<CustomerModel, CustomerModelMapper>(@"customers.csv");

Name: | |

—|—|—

Header: | |

Separator: | |

Mapping type: | | Map model to field position Map model to field names

C# model and mapper classes:

Written by Tedd · Oslo, Norway

Updated 7 Oct 2020

Follow the journal via RSS