CSV and Excel if one of the widely used formats to save data, so if you are working with CSV files, you may want to read data from CSV file in C# or load and read CSV data using C#, so in this article, I have mentioned how we can open and read CSV data in C# using Console application with various possible ways.
Suppose we have CSV as below
Read CSV using StreamReader
In this method, we will simply load the CSV file using StreamReader and read it line by line, while splitting values by ",".
Once we have values, we will print them line by line, here is the code with comments.
using System;
using System.Collections.Generic;
using System.IO;
namespace ReadCSV
{
internal class Program
{
static void Main(string[] args)
{
// load CSV in reader
using (var reader = new StreamReader(@"D:\Orders.csv"))
{
int i = 1;
//read CSV file line by line until End of stream
while (!reader.EndOfStream)
{
//read single line
var line = reader.ReadLine();
//split values by ','
var values = line.Split(',');
if (i == 1)
{
//read and print headers at first
PrintValues(values);
i++;
}
else
{
//read and print headers rows
PrintValues(values);
}
Console.WriteLine();
}
}
}
public static void PrintValues(string[] values)
{
foreach (var val in values)
Console.Write(val + " ");
}
}
}
Output:
Id Country Price OrderQuantity
1 India 10.00 4
2 Australia 5.00 10
3 Brazil 10.00 5
4 China 5.50 5
5 Nepal 20.20 10
That's it, as you can see we were able to load and read the CSV file in C# using StreamReader.
Read CSV using Microsoft.VisualBasic
For Using this method, first, you will have to add a reference to Microsoft.VisualBasic.
If you are using .NET Core, you can simply go to "Tools" -> "Nuget package manager" -> "Nuget Package for solution" and then search for "Microsoft.VisualBasic"
Once you have installed the code, you can use below code to read CSV
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
namespace ReadCSV
{
internal class Program
{
static void Main(string[] args)
{
int i = 1;
var path = @"D:\Orders.csv"; // Habeeb, "Dubai Media City, Dubai"
using (TextFieldParser csvParser = new TextFieldParser(path))
{
csvParser.SetDelimiters(new string[] { "," });
csvParser.HasFieldsEnclosedInQuotes = true;
while (!csvParser.EndOfData)
{
// Read current line fields, pointer moves to the next line.
string[] fields = csvParser.ReadFields();
if (i == 1)
{
//read and print headers at first
PrintValues(fields);
i++;
}
else
{
//read and print headers rows
PrintValues(fields);
}
Console.WriteLine();
}
}
}
public static void PrintValues(string[] values)
{
foreach (var val in values)
Console.Write(val + " ");
}
}
}
Output is the same as above
Id Country Price OrderQuantity
1 India 10.00 4
2 Australia 5.00 10
3 Brazil 10.00 5
4 China 5.50 5
5 Nepal 20.20 10
Read CSV using Linq
Linq is the shortest and easiest method to read CSV in C#, you can simply read all lines using one line 'File.ReadAllLines(path).Select(a => a.Split(','));
', here is the complete code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ReadCSV
{
internal class Program
{
static void Main(string[] args)
{
var path = @"D:\Orders.csv";
//read all lines at once and get it as list
var lines = File.ReadAllLines(path).Select(a => a.Split(','));
//loop through each line, one by one
foreach(var line in lines)
{
PrintValues(line);
Console.WriteLine();
}
}
public static void PrintValues(string[] values)
{
foreach (var val in values)
Console.Write(val + " ");
}
}
}
Output is the same as above.
You may also like to read: