A helpful tool to - Convert CSV to class object - Convert CSV to DataTable - Convert Class object to a CSV format
$ dotnet add package CsvProcessorCSV Processor is a library that allows the processing CSV files.
Supported platforms: .NET Core 3.1
dotnet add package CsvProcessor
Browse the CsvProcessor on NuGet.
Types are in the CsvProcessor namespace
using CsvProcessor;
The root Processor is created using CsvProcessor.Processor.
string path = "[CSV File Directory]";
Processor<FileTypes> processor = new Processor<FileTypes>(path, FileTypes.Countries);
Note that FileType could be any enumerables that you have set in your project.
There are 3 ways to use treat a CSV file with the CsvProcessor
Step 1: Defining the DataModel (Example will be using an ISO standard CSV)
public class Country
{
[Csv("0")]
public string Name { get; set; }
[Csv("1")]
public string AlphaCode2 { get; set; }
[Csv("2")]
public string AlphaCode3 { get; set; }
[Csv("3")]
public int NumericalCode { get; set; }
[Csv("4")]
public double Latitude { get; set; }
[Csv("5")]
public double Longitude { get; set; }
}
_Note that we make use of the CsvAttribute, which takes only one parameter: Position, to define the column position of the different properties in the CSV File.
Step 2: Converting the CSV File to a IEnumerable<Country> which can later be converted to a List<Country> afterward if there's ever a need.
string path = "[CSV File Directory]";
Processor<FileTypes> processor = new Processor<FileTypes>(path, FileTypes.Countries);
IEnumerable<Country> countries = processor.Execute<Country>();
//Optional to List
List<Country> countries = processor.Execute<Country>().ToList();
string path = "[CSV File Directory]";
Processor<FileTypes> processor = new Processor<FileTypes>(path, FileTypes.Countries);
DataTable dt = processor.GetContentAsDataTable();
Step 1: Defining the custom processor by inheriting the interface IProcessor which will automatically convert the file into a DataTable and will append Column with the column Index. For example, the first column in the file will be named Column0.
public class CountryProcessor : IProcessor
{
void IProcessor.Process(DataTable dt)
{
// Processor Logic after DataTable conversion
foreach (DataRow row in datatable.Rows)
{
Country country = new Country()
{
Name = row["Column0"].ToString(),
AlphaCode2 = row["Column1"].ToString(),
AlphaCode3 = row["Column2"].ToString(),
NumericalCode = Convert.ToInt32(row["Column3"].ToString()),
Latitude = Convert.ToDouble(row["Column4"].ToString()),
Longitude = Convert.ToDouble(row["Column5"].ToString())
};
}
}
}
Step 2: Processing the file
string path = "[CSV File Directory]";
FileTypeInit.FileTypes.Add(FileTypes.Countries, new CountryProcessor());
Processor<FileTypes> processor = new Processor<FileTypes>(path, FileTypes.Countries);
processor.Execute();
CSV Processor is open source under the MIT license and is free for commercial use.
Thanks to Shayl Sawmynaden for pitching the idea.