In this tutorial we will se how to read and write data to csv file. It is pretty much easy if you have some external library for that. We are definitely going to use a library and that will be CsvHelper.
You can also check the video version of this tutorial.
First and foremost, create a c# console application in .net core. After that we need to install a nuget package, which is**CsvHelper**
In Program.cs
file, remove the boiler plate code and add this one.
namespace CsvReaderDemo
public class Program
{
static void Main() {
}
}
Now we will create a class Person in the same file.
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Country { get; set; }
}
Inside the class Program, we will create a static method WriteToCsv
static void WriteToCsv(string filePath)
{
try
{
if (!File.Exists(filePath))
{
using (File.Create(filePath)) { }
}
List<Person> people = new List<Person>
{
new Person { Name = "Jane", Age = 25, Country = "UK" },
new Person { Name = "Any", Age = 30, Country = "Canada" },
new Person { Name = "Mike", Age = 35, Country = "USA" }
};
var configPersons = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false
};
using (StreamWriter streamWriter = new StreamWriter(filePath,true))
using (CsvWriter csvWriter = new CsvWriter(streamWriter, configPersons))
{
csvWriter.WriteRecords(people);
}
Console.WriteLine("Data written to CSV successfully.");
}
catch(Exception ex)
{
throw;
}
}
Lets breakdown it.
π This method takes a parameter filePath of string type.
π First we will check if this file exists or not, if it does not exist we need to create that file.
if (!File.Exists(filePath))
{
using (File.Create(filePath)) { }
}
π using
keyword tells us that we do not need to worry about disposal of theobject.
var configPersons = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false
};
πHere we are gonna write some configuration for csv helper,
To this object we are passing a value, CultureInfo.InvariantCulture, it means we are not culture dependent right now.
HasHeaderRecord = false
π HasHeaderRecord tells whether we need a header or not in a file. So lets set it to false.
using (StreamWriter streamWriter = new StreamWriter(filePath,true))
π We are creating instance of StreamWriter, it is taking two arguments first one in filePath, and second argument indicates whether or not you want to append data to a file or not. We are setting it to true.
using (CsvWriter csvWriter = new CsvWriter(streamWriter, configPersons))
{
csvWriter.WriteRecords(people);
}
π Her we are creating the instance of CsvWriter, it will take two parameters first one is streamWriter and another one is config options.
Lets write people to file.
π Make sure to use these name spaces in the top.
using CsvHelper.Configuration;
using CsvHelper;
using System.Globalization;
Let go to the main method , call WriteToCsv method and pass the filePath.
static void Main()
{
string directoryPath = @"C:\\Users\\RD\\Documents\\Projects";
string fileName = "data.csv";
string filePath = Path.Combine(directoryPath, fileName);
WriteToCsv(filePath);
}
Now lets see how to read data from this file. We will create a method ReadFromCsv and it will take one argument filePath of string type.
static void ReadFromCsv(string filePath)
{
try
{
if (File.Exists(filePath))
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false
};
using (StreamReader streamReader = new StreamReader(filePath))
using (CsvReader csvReader = new CsvReader(streamReader, config))
{
// Read records from the CSV file
IEnumerable<Person> records = csvReader.GetRecords<Person>();
// Process each record
foreach (Person person in records)
{
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}, Country: {person.Country}");
}
}
}
}
catch (Exception ex)
{
throw;
}
}
Letβs break it down.
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false
};
π It is the config option for csv reader.
CultureInfo.InvariantCulture says that we are not dependent on any culture.
HasHeaderRecord=false says that we are not looking for any header to read.
Lets call this method in Main method.
static void Main()
{
string directoryPath = @"C:\\Users\\RD\\Documents\\Projects";
string fileName = "data.csv";
string filePath = Path.Combine(directoryPath, fileName);
// WriteToCsv(filePath);
ReadFromCsv(filePath);
Console.ReadKey();
}
So now we can see that, how easy is to read and write to csv file and all thanks to the csv helper library.
πSource code: https://github.com/rd003/CsvReaderDemo
Originally posted by Ravindra Devrani on May 20, 2023.
Exported from Medium on February 19, 2025.