Read data from a CSV files or Tab delimited file is very common especially in the era of big data where you get a large flat file exported, transformed and then reloaded. I am often shocked to see that people write their own algorithm for this when a very powerful module already exists and is free.
CSVhelper is available on GitHub and in its post basic usage you create a class file in your solution with the field name matching the fields in your Comma Separated file. Then you simply do this:
- Create a class to store your data
class myRecord
{
public string Id { get; set;}
public string Name {get; set;}
}
- Write code to open your file stream
- Reference the CsvHelper dll
- place a using statement for the CsvHelper at the top of your module
using CsvHelper;
- Use code similar to this where myFileStream is the stream you opened and myRecord is your class to fill with the data:
using (var csv = new CsvReader(myFileStream)) {
var recordList = csv.GetRecords().ToList();
}
- Voila, the variable recordList is a list that contains an instance of myRecord per row in your file.
Now this will read a comma-separated file but what if your file is tab delimited. Well that is just as simply create a configuration object and assign delimiter:
CsvConfiguration config = new CsvConfiguration();
config.Delimiter = "\t";
Now pass this configuration object into your call:
using (var csv = new CsvReader(myFileStream, config))
{
var recordList = csv.GetRecords().ToList();
}
This same library can be used to write to a CSV to Tab Delimited file. You would make a call something like this:
// Write the file
using (var swFlat = new StreamWriter(connectionString))
{
using (var csvwriteFlat = new CsvWriter(swFlat))
{
if (recordList != null) csvwriteFlat.WriteRecords(recordList);
}
}