How to Read and Write csv file in C#
By FoxLearn 7/16/2024 8:54:47 AM 18.49K
To read and write data to a CSV file in C# you can use the CsvHelper library.
CsvHelper is a popular .NET library that simplifies the reading and writing of CSV files in C#. It handles various CSV parsing and formatting complexities such as handling different field delimiters, quoting, and escaping characters.
Open your Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Enter your project name and then click OK button.
How to read and write data to csv file in C#
You need to first install the CsvHelper package through NuGet. Right click on your project select Manage NuGet Packages -> Search csvhelper -> Install
This is a library for reading and writing CSV files. It's extremely fast, flexible, and easy to use. Supports reading and writing of custom class objects.
Drag and drop Button, DataGridView control from the Visual Studio toolbox into your form, then design a simple UI that allows you to open a csv file, then display your csv data in DataGridView, and when you click Save button it will save data to csv file as below.
We will create a Student class to map data as below
public class Student { public string StudentID { get; set; } public string StudentName { get; set; } public string Email { get; set; } public string Phone { get; set; } }
Add a Form_Load event handler allows you to set bindingsource to new student list.
private void Form1_Load(object sender, EventArgs e) { //Load data to binding source studentBindingSource.DataSource = new List<Student>(); }
How to read csv file in c#
When you click the Read button, the OpenFileDialog will show up allowing you to select the csv file. We will use the StreamReader to open csv file in c#, CsvReader to read the csv file in c#, then import the csv into the DataGridView.
//c# read data from csv file private void btnRead_Click(object sender, EventArgs e) { using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "CSV|*.csv", ValidateNames = true }) { if (ofd.ShowDialog() == DialogResult.OK) { //Read data from CSV file, then fill data from csv file to datagridview var sr = new StreamReader(new FileStream(ofd.FileName, FileMode.Open)); var csv = new CsvReader(sr); studentBindingSource.DataSource = csv.GetRecords<Student>();//Read the records into a list } } }
How to write data to csv in c#
When you click the Save button, the SaveFileDialog will show up allowing you to enter a csv name. We will use the StreamWriter, CsvWriter to write data to the csv file in c#, this is the simple way to export data from DataGridView to csv file in c#
// c# write data to csv private void btnWrite_Click(object sender, EventArgs e) { using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "CSV|*.csv", ValidateNames = true }) { if (sfd.ShowDialog() == DialogResult.OK) { //c# write data to CSV file, save data from datagridview to csv file using (var sw = new StreamWriter(sfd.FileName)) { var writer = new CsvWriter(sw); writer.WriteHeader(typeof(Student)); foreach (Student s in studentBindingSource.DataSource as List<Student>) { writer.WriteRecord(s); } } MessageBox.Show("Your data has been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
Through this example, you have learned how to create a csv file in c#, as well as how to read a csv file easily by using the CsvHelper library.
VIDEO TUTORIALS