Home > Uncategorized > Working with CSV file in C#.NET

Working with CSV file in C#.NET

December 15th, 2012

Let us say, we have a csv file with 5 columns and 4 rows. Since CSV file contains data that we often wish to work, we would go through this tutorial to see how we can read, access the CSV file data. Here is the test.csv file the we want to work on.

car

Here is the C#.NET code to accomplish this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var reader = new StreamReader(File.OpenRead(@"C:\charp\test.csv"));

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                Console.WriteLine(line); 
            }

            Console.ReadLine();
        }
    }
}

There is nothing great in this program. We use the simple StreamReader access the file and then read line by line using Readline. Finally we print the line using Console.Writeline().

Let us now extend our extending. The elements in a row are separated by comma. Let us assume that we wish read and write the line that has car name as "Honda". Here is the program to accomplish that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var reader = new StreamReader(File.OpenRead(@"C:\csharp\test.csv"));
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');
                if (values[1] == "Honda")
                {
                    Console.WriteLine(line);
                }
            }
            Console.ReadLine();
        }
    }
}

The line

 var values = line.Split(','); 

Splits the line and assigns the individual elements in the array values. The values[0] contain the first column element and the values[1] will contain the second row element.We compare if the car is Honda using the following

if (values[1] == "Honda") 

Exercise for Readers

Try to modify the code such that it will print the two lines that match to two given Car names. Here is the solution

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var reader = new StreamReader(File.OpenRead(@"C:\projects_vikas\Vikas_websites\compare-processor\csharp\test.csv"));
string processor1_input = "Toyota";
string processor2_input = "Honda";
string processor1 ="";
string processor2 ="";
int number_of_matches = 0;
int i;

while ((!reader.EndOfStream) && number_of_matches <=1)
{
var line = reader.ReadLine();
var values = line.Split(',');
if( (values[1] == processor1_input) || (values[1] == processor2_input))
{
if (number_of_matches == 0) processor1 = line; else processor2 = line;
number_of_matches++;
//Console.WriteLine(line);
}
}

for (i=0; i<number_of_matches; i++)
{
if (i == 0) Console.WriteLine(processor1); else Console.WriteLine(processor2);
}

Console.ReadLine();
}
}
}

Uncategorized

  1. No comments yet.
  1. No trackbacks yet.