דפים

12/24/12

Parse TabDelimited text file

public static System.Data.DataTable ReadTabDelimeted(string path)
        {
            StreamReader sr = new StreamReader(path);
            System.Data.DataTable dt = new System.Data.DataTable();
            try
            {               
                if (sr.Peek() > 0)
                {
                    string[] headers = sr.ReadLine().Split('\t');
                    foreach (string header in headers)
                        dt.Columns.Add(header);
                }              
                while (sr.Peek() > 0)
                {
                    string[] cells = sr.ReadLine().Split('\t');
                    DataRow dr = dt.NewRow();
                    for (int i = 0; i < cells.Length; i++)
                    {
                        dr[i] = cells[i];
                    }
                    dt.Rows.Add(dr);
                }
            }
            finally
            {
                sr.Close();
            }
         
            return dt;
        }

No comments:

Post a Comment