CSV to DataTable

Converting Microsoft Excel CSV to .Net DataTable:
After casually browsing the net for some good examples, everything I came across was too bulky or over-kill for what I needed this code for, so I decided to write my own.

The following code snippet is tested and works with .Net 2.0 or later. Please note “handleError()” method which is not defined in code below. This is simply generic static method accepting string and exception types to handle all exceptions in your application in a uniform manner.

The nice thing about this code compared to some other examples you may come across in your searches is that it works stand-alone without any external references or potentially buggy usage/constructors such as OleDB Jet provider.

It’s clean, concise, and to the point. Use it wisely. 😉

private DataTable MyCSVtoDT(string FileNameWithPath)
{
    try {
        bool HeaderColumnsAdded = false;
        DataTable dt = new DataTable();
       
        StreamReader reader = File.OpenText(FileNameWithPath);
        string line = reader.ReadLine();
        while ((line != null)) {
            int colcounter = 0;
            DataRow row = dt.NewRow();
            foreach (string colvalue in line.Split(“,”)) {
                if (HeaderColumnsAdded == false) {
                    dt.Columns.Add();
                }
                row(colcounter) = colvalue;
                colcounter += 1;
            }
            HeaderColumnsAdded = true;
            dt.Rows.Add(row);
            line = reader.ReadLine();
        }
        reader.Close();
       
        return dt;
    }
    catch (Exception ex0) {
        handleError(ex0, “Ex0::MyCSVtoDT”);
        throw new Exception(ex0.Message, ex0.InnerException);
        return null;
    }
}

About Ronnie Diaz

Ronnie Diaz is a software engineer and tech consultant. Ronnie started his career in front-end and back-end development for companies in ecommerce, service industries and remote education. This work transitioned from traditional desktop client-server applications through early cloud development. Software included human resource management and service technician workflows, online retail e-commerce and electronic ordering and fulfillment, IVR customer relational systems, and video streaming remote learning SCORM web applications. Hands on server experience and software performance optimization led to creation of a startup business focused on collocated data center services and continued experience with video streaming hardware and software. This led to a career in Amazon Prime Video where Ronnie is currently employed, building software and systems which stream live sports and events for millions of viewers around the world.

Posted on March 2, 2010, in Programming & Development. Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: