Extracting Data from a CSV File Using C#

Have you ever found yourself in a position of wanting to extract data from a CSV file that had way more information than you needed? Here is the code I used to extract a single column and output it into a windows form textbox.

This first chunk contains a partial class of the form class named Form1 in my Windows Form Application. I’ve commented in some explanation of how it works.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO; // used for creating and writing text to a file

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /// This part instantiates the OpenFileDialog object so we can show it on the screen when button1 is clicked
            /// and use the resulted chosen file
            OpenFileDialog FileDialog = new OpenFileDialog(); 
            textBox1.Text = "";
            richTextBox1.Text = "";
            DialogResult result = FileDialog.ShowDialog();
            if (result == DialogResult.OK) 
            {
                // Dump the selected file line by line into the "row" array so that each element of "row" is one line of the file
                string[] row = File.ReadAllLines(FileDialog.FileName);

                // Determine the number of rows in the file then outpute it into a text box
                int numberOfRows = File.ReadAllLines(FileDialog.FileName).Length; 
                textBox1.Text = numberOfRows.ToString();

                /// Separate the headings of the CSV file (which which we assume to be in the 1st row) according to our chosen delimiters and place them into the "firstRow" array
                char[] delimiterChars = { ',', '\t' }; //both comma and tab delimiters are accepted
                string[] firstRow = row[0].Split(delimiterChars);

                //some variables
                string extension = ""; //place the extension values here
                int columnNumber = 0; //keep track of which column we are in

                // main logic for extracting data under the "extension" column
                foreach (string value in firstRow)// sequencially places each item of firstRow into "value" and runs through the logic for each iteration
                {
                    if (value == "extension" || value == "Extension") //Determine which column the "extension" heading is located in
                    {
                        richTextBox1.Text += "The " + value + " heading is in column " + columnNumber + " and it's values are:\r";
                        for (int i = 1; i < numberOfRows; i++) // loops through each line and picks out the extension identified by "columnNumber"
                        {
                            string[] lineValue = row[i].Split(delimiterChars);
                            extension = lineValue[columnNumber];
                            richTextBox1.Text += extension + "\r";
                        }
                    }
                    columnNumber++;
                }
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *