CSV files are used for sharing and consuming data with other software applications. It is a common file format.CSV contains multiple lines, and within each line, values are separated by a comma, semicolon, or some other delimiter.
Below is a sample CSV file :
I will write a C# code to create a CSV file. I will use the CSVHelper NuGet package for this code.
Step 1. Open Visual Studio and create a console project.
Click on the option ‘Create a new project’. After that below screen will appear.
Select the highlighted option Console App (.Net Framework) and click on the Next button. Please make sure that you have selected the C# version.
Provide the project name and click on Create button. A console project is created.
Step 2. Now we need to add the CSVHelper NuGet package. Click on the Tools menu in Visual Studio and select the option ‘Manage Nuget Packages for Solution’.
Under the Browse tab, search for the CSVHelper package and click on the Install button.
Step 3. Add Student class to the project. Below is the code.
public class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
Step 4. Prepare a list of students for writing in a CSV file. In the main() method of the Program.cs file, add code to prepare a list of students. Please see the below code for creating a list of students.
class Program {
static void Main(string[] args) {
var studentObjects = new List < Student > () {
new Student {
FirstName = "Rahul", LastName = "Kumar", Email = "[email protected]"
},
new Student {
FirstName = "Ayush", LastName = "Kumar", Email = "[email protected]"
},
new Student {
FirstName = "Surendra", LastName = "Singh", Email = "[email protected]"
}
};
}
}
Step 5. Writing Student details in CSV file. Now we will write code for creating a CSV file that will contain student details. Add a namespace of CSVHelper, which contains objects for working with CSV files. Below is the code to add namespace.
using CsvHelper;
We need to add 2 more namespaces; below is the code.
using System.Globalization;
using System.IO;
Please see the below code, which is added in the main() method of the “Program.cs” class for writing CSV files.
static void Main(string[] args) {
var studentObjects = new List < Student > () {
new Student {
FirstName = "Rahul", LastName = "Kumar", Email = "[email protected]"
},
new Student {
FirstName = "Ayush", LastName = "Kumar", Email = "[email protected]"
},
new Student {
FirstName = "Surendra", LastName = "Singh", Email = "[email protected]"
}
};
using(var writer = new StreamWriter(@ "F:\Projects\CSVFiles\students.csv"))
using(var csv = new CsvWriter(writer, CultureInfo.InvariantCulture)) {
csv.WriteRecords(studentObjects);
}
}
In the above code, I have provided the hard-coded location for the CSV file. Please change the path according to your computer drive.
Now our code is complete. Click on the start button to start debugging of console application. Now check after the console application’s execution is complete. You will find a new CSV file created at the location specified in the code.
Explanation of code
In the above code, we have prepared a list of Students. After that, we used StreamWriter class which handles writing to CSV files. It is defined in System.IO namespace. We have also used CsvWriter, which is included in CSVHelper. We have used CultureInfo.InvariantCulture to write CSV in a culture-independent way.