In this tutorial you will learn How to Read CSV File in C# Console. Firstly we must create for read a CSV file. The following LoadCSV method reads the CSV file into a two – dimensional array of strings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
private static string[,] LoadCSV(string filename) { // Get the file's text. string whole_file = System.IO.File.ReadAllText(filename); // Split into lines. whole_file = whole_file.Replace('\n', '\r'); string[] lines = whole_file.Split(new char[] { '\r' }, StringSplitOptions.RemoveEmptyEntries); // See how many rows and columns there are. int num_rows = lines.Length; int num_cols = lines[0].Split(',').Length; // Allocate the data array. string[,] values = new string[num_rows, num_cols]; // Load the array. for (int r = 0; r < num_rows; r++) { string[] line_r = lines[r].Split(','); for (int c = 0; c < num_cols; c++) { values[r, c] = line_r[c]; } } // Return the values. return values; } |
The code uses System.IO.File.ReadAllText to read the file’s contents into a string. It then uses Split to break the file into lines, ignoring any blank lines.
The code then loops through the lines using Split to split the lines into fields and adding their values to the array. When it’s done, the method returns the two-dimensional array of strings.
When Main method load, the following code calls the LoadCSV method and displays the result on Console Screen.
I used this sample file
Solution 1: Write all data into Console
This code calls LoadCSV and saves the returned strings in an array. It uses the array’s GetUpperBound method twice to see how many rows and columns it contains.
Next the program loops through the first values in each of the array’s columns and uses it to build a column in Console. For each value, it creates a column named after the value and displaying the value as its header text.
Next the code loops through the remaining rows in the table. For each row, it adds the row’s values into Console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
class Program { static void Main(string[] args) { // Get the data from path. string sampleCSV = @"D:/sample.csv"; string[,] values = LoadCSV(sampleCSV); int num_rows = values.GetUpperBound(0) + 1; int num_cols = values.GetUpperBound(1) + 1; // Display the data to show we have it. for (int c = 0; c < num_cols; c++) Console.Write(values[0, c]+"\t"); //Read the data. for (int r = 1; r < num_rows; r++) { // dgvValues.Rows.Add(); Console.WriteLine(); for (int c = 0; c < num_cols; c++) { Console.Write(values[r, c] + "\t"); } } Console.ReadLine(); } private static string[,] LoadCSV(string filename) { // Get the file's text. string whole_file = System.IO.File.ReadAllText(filename); // Split into lines. whole_file = whole_file.Replace('\n', '\r'); string[] lines = whole_file.Split(new char[] { '\r' }, StringSplitOptions.RemoveEmptyEntries); // See how many rows and columns there are. int num_rows = lines.Length; int num_cols = lines[0].Split(',').Length; // Allocate the data array. string[,] values = new string[num_rows, num_cols]; // Load the array. for (int r = 0; r < num_rows; r++) { string[] line_r = lines[r].Split(','); for (int c = 0; c < num_cols; c++) { values[r, c] = line_r[c]; } } // Return the values. return values; } } |
Solution 2: Create custom object for each elements from CVS and Fill a LIST with custom objects
Create Person class as following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Person { string name; string mail; string company; int age; public Person(string n, int a, string m, string c) { name = n; mail = m; company = c; age = a; } public string Name { get => name; set => name = value; } public string Mail { get => mail; set => mail = value; } public string Company { get => company; set => company = value; } public int Age { get => age; set => age = value; } } |
Creating Main method and LoadCSV codes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
class Program { static List<Person> list; static Person person; static void Main(string[] args) { list = new List<Person>(); // Get the data from path. string sampleCSV = @"D:/sample.csv"; string[,] values = LoadCSV(sampleCSV); int num_rows = values.GetUpperBound(0) + 1; //Read the data and add to List for (int r = 1; r < num_rows; r++) { //name age mail company person = new Person(values[r, 0], int.Parse(values[r, 1]), values[r, 2], values[r, 3]); list.Add(person); } //read data from list foreach (var item in list) { Console.WriteLine(item.Name+"\t"+ item.Age + "\t" + item.Company + "\t" + item.Mail + "\t" ); } Console.ReadLine(); } private static string[,] LoadCSV(string filename) { // Get the file's text. string whole_file = System.IO.File.ReadAllText(filename); // Split into lines. whole_file = whole_file.Replace('\n', '\r'); string[] lines = whole_file.Split(new char[] { '\r' }, StringSplitOptions.RemoveEmptyEntries); // See how many rows and columns there are. int num_rows = lines.Length; int num_cols = lines[0].Split(',').Length; // Allocate the data array. string[,] values = new string[num_rows, num_cols]; // Load the array. for (int r = 0; r < num_rows; r++) { string[] line_r = lines[r].Split(','); for (int c = 0; c < num_cols; c++) { values[r, c] = line_r[c]; } } // Return the values. return values; } } |
Output:
How do you not have a return in the Person class? I’m getting an error there.
Also can’t do int.Parse