This is an static method to return a datatable . The CSV by default has headers.

  public static DataTable csvParser(string csvPath)
            {
                var sb = new StringBuilder();
                var reader = new StreamReader(System.IO.File.OpenRead(csvPath));
                var headerline = reader.ReadLine();
                var headers = headerline.Split(',');
                DataTable dt = new DataTable();
                for (int i = 0; i < headers.Length; i++)
                {
                    dt.Columns.Add(headers[i].Trim('\"'), typeof(String));
                }
                int currentColumn, start;
                StringBuilder sb2 = new StringBuilder();
                String[] row = new String[headers.Length];
                currentColumn = 0;
                start = 0;
                sb2.Clear();
                char ch;
                while (!reader.EndOfStream)
                {
                    ch = (char)reader.Read();
                    switch (ch)
                    {
                        case '\"':
                            if (start == 0)
                                start = 1;
                            else
                                start = 0;
                            break;

                        case ',':
                            if (start == 1)
                                sb2.Append(' ');

                            else
                            {
                                row[currentColumn] = sb2.ToString();
                                currentColumn = currentColumn + 1;
                                sb2.Clear();

                            }

                            break;
                        case '\n':
                            if (start == 1)
                            {
                                sb2.Append(' ');
                                break;
                            }

                            if (currentColumn < headers.Length - 1)
                                break;
                            else
                            {
                                row[currentColumn] = sb2.ToString();
                                sb2.Clear();
                                currentColumn = 0;
                                DataRow workRow = dt.NewRow();
                                for (int k = 0; k < headers.Length; k++)
                                {
                                    workRow[headers[k].Trim('\"')] = row[k];

                                }
                                    dt.Rows.Add(workRow);
                                break;
                            }
                        case '\r':
                            if (start == 0)
                            {
                                sb2.Append(' ');
                                break;
                            }

                            if (currentColumn < headers.Length - 1)
                                break;
                            else
                            {
                                row[currentColumn] = sb2.ToString();
                                sb2.Clear();
                                currentColumn = 0;
                                DataRow workRow = dt.NewRow();
                                for (int k = 0; k < headers.Length; k++)
                                {
                                    workRow[headers[k].Trim('\"')] = row[k];

                                }
                                    dt.Rows.Add(workRow);
                                break;
                            }
                        case ' ':
                            sb2.Append(' ');
                            break;
                        default:
                            sb2.Append(ch.ToString());
                            break;

                    }
                }
                return dt;
            }

 

About Lei

I am an IT specialist with over 10 year experience - years on Automation, on-Premise or Azure.

I am happy to develop however never want be a full time developer. Only do what I have to do. If it has to be PowerShell,HTML, PHP, CSS, C#, VBS or JS, front end or backend, so be it, doesn't matter!

Spent years with Windows, SCCM, SharePoint, SQL and Exchange servers. For last several years, I have been actively working under On Premise > Azure environment.

THERE IS NO WAY BACK!!!

Current Certificates:
    Microsoft® Certified-
  • -Enterprise Administrator
  • -Database Administrator
  • -SharePoint Administrator
  • -Administering and Deploying SCCM 2012
Red Hat Certified Technician
ITIL V3 Foundation - Practitioner

Working on Azure Certificates now and hopefully they can stop upgrading their questions one day! GIVE ME A BREAK!

Contact Lei

Name *
Email *
Comments *

Traffic since 10/08/2016

Today47
Yesterday83
This week671
This month1967
Total416327

Visitor Info

  • IP: 54.227.51.103
  • Browser: Unknown
  • Browser Version:
  • Operating System: Unknown

Who Is Online

1
Online

2017-12-17

Login