Read and Write a CSV File in ASP.NET Core

Introduction 

 
For this blog, my agenda is to provide a step-by-step solution to read and write CSV files in ASP.NET Core 3.0, and CSVHelper. The Same Logic will work for Web Application, Windows Application, and Console Application. In this post, I'm going to create a sample Console Application to show you the process.
 
Steps:
  1. Create a Console Application project
  2. Create a student class inside the project
  3. Install the CSVHelper from NuGet Package Manager
  4. Add Mappers folder and inside add a mapper Class for Student
  5. Add Services Folder in the project and Add StudentService Class inside it
  6. Write the Logic inside the main method of Program file as its starting point of the application
To watch the video of the same tutorial, click the link.
 
Create a Console Application project
 
Give the Name to The Project as "ReadWriteCSVFile". You can give any name.
 
Create a student class inside the project 
 
Write the below code inside the Student class:
  1. namespace ReadWriteCSVFile {  
  2.     public class Student {  
  3.         public int RollNo {  
  4.             get;  
  5.             set;  
  6.         }  
  7.         public string Name {  
  8.             get;  
  9.             set;  
  10.         }  
  11.         public string Course {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public decimal Fees {  
  16.             get;  
  17.             set;  
  18.         }  
  19.         public string Mobile {  
  20.             get;  
  21.             set;  
  22.         }  
  23.         public string City {  
  24.             get;  
  25.             set;  
  26.         }  
  27.     }  
  28. }  
In the next step, we are going to install the CSVHelper package so that it will help us to read and write the CSV file in an easy way.
 
Now Install the CSVHelper from NuGet Package Manager --Version (12.2.1),
  • Step 1 - Right Click on the Project
  • Step 2 - Go To "Manage NuGet Packages..."
  • Step 3 - Go To Browse Tab then move to the Search area
  • Step 4 - Type "CSVHelper" in the search box
Here you will see a list of packages. Choose the first one and move it to the right-side panel.
 
You will see one option as Version: (If not installed, if you already installed, then you will see both Installed and Version, two options)
Select Version 12.2.1 and click on the install button and follow the steps to install successfully.
 
Add "Mappers" folder and inside it add mapper Class for Student
  • Here you can give any name to the folder and class
  • Give the proper name to the class as "StudentMap"
  • Make this class as sealed
Write the below code inside the StudentMap Class:
  1. namespace ReadWriteCSVFile.Mappers {  
  2.     public sealed class StudentMap: ClassMap < Student > {  
  3.         public StudentMap() {  
  4.             Map(x => x.RollNo).Name("RollNo");  
  5.             Map(x => x.Name).Name("Name");  
  6.             Map(x => x.Course).Name("Course");  
  7.             Map(x => x.Fees).Name("Fees");  
  8.             Map(x => x.Mobile).Name("Mobile");  
  9.             Map(x => x.City).Name("City");  
  10.         }  
  11.     }  
  12. }  
Add "Services" Folder in the project and Add StudentService Class inside it:
  • Here, you can give any name to the folder and class
  • Give the proper name to the class as "StudentService"
Write the below Code inside the StudentService Class
  1. namespace ReadWriteCSVFile.Services {  
  2.     public class StudentService {  
  3.         public List < Student > ReadCSVFile(string location) {  
  4.             try {  
  5.                 using(var reader = new StreamReader(location, Encoding.Default))  
  6.                 using(var csv = new CsvReader(reader)) {  
  7.                     csv.Configuration.RegisterClassMap < StudentMap > ();  
  8.                     var records = csv.GetRecords < Student > ().ToList();  
  9.                     return records;  
  10.                 }  
  11.             } catch (Exception e) {  
  12.                 throw new Exception(e.Message);  
  13.             }  
  14.         }  
  15.         public void WriteCSVFile(string path, List < Student > student) {  
  16.             using(StreamWriter sw = new StreamWriter(path, falsenew UTF8Encoding(true)))  
  17.             using(CsvWriter cw = new CsvWriter(sw)) {  
  18.                 cw.WriteHeader < Student > ();  
  19.                 cw.NextRecord();  
  20.                 foreach(Student stu in student) {  
  21.                     cw.WriteRecord < Student > (stu);  
  22.                     cw.NextRecord();  
  23.                 }  
  24.             }  
  25.         }  
  26.     }  
  27. }  
Write the Logic inside the main method of Program file as its starting point of the application.
 
Change the code inside the Main method of Program class as shown below:
  1. namespace ReadWriteCSVFile {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             Console.WriteLine("Start CSV File Reading...");  
  5.             var _studentService = new StudentService();  
  6.             var path = @ "D:\Tutorials\Student.csv";  
  7.             //Here We are calling function to read CSV file  
  8.             var resultData = _studentService.ReadCSVFile(path);  
  9.             //Create an object of the Student class  
  10.             Student student = new Student();  
  11.             student.RollNo = 5;  
  12.             student.Name = "Lucy";  
  13.             student.Course = "B.Tech";  
  14.             student.Fees = 75000;  
  15.             student.Mobile = "7788990099";  
  16.             student.City = "Pune";  
  17.             resultData.Add(student);  
  18.             //Here We are calling function to write file  
  19.             _studentService.WriteCSVFile(@ "D:\Tutorials\NewStudentFile.csv", resultData);  
  20.             //Here D: Drive and Tutorials is the Folder name, and CSV File name will be "NewStudentFile.csv"  
  21.             Console.WriteLine("New File Created Successfully.");  
  22.         }  
  23.     }  
  24. }  
I hope this tutorial will help you to achieve your target. If you found this helpful, then please like and provide your feedback. Thanks for reading!