Skip to content

.txt / csv Header (FirstLine) to Array

BASCHDI

Asked Jul 1, 2013, 1:50 AM EDTSolved
Share
LinkedInX
Hello,

anyone could help me out with a snippet?

I have a Tab delimited .CSV.

I want to parse the words, delimited by tabs, into a seperate Array for everyone.


File looks like this:


SKU        Name Available ShortDescription      



FirstLine only have column Names.

6 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Sunny  Sharma
    ✓ Accepted answer0
    Hello BASCHDI,

    Well, you can do so using Regex.Split() method, but this way you will fail to maintain the consistency of data. There are chances of you might mess up the data for a particular SKU with the details of some other SKU. Hence I still would suggest you to use Oledb Reader.

    Below is a solution for you to import a text file directly into a DataTable:
    ---------------------------------------------------------------------

    static DataTable ImportDelimitedFile(DataTable dt, string filename, string delimiter)  
    {
     StreamReader file = new StreamReader(filename);    string line;
     while ((line = file.ReadLine()) != null)  {  if (line.Trim().Length > 0)  {
     string[] columns = line.Split(delimiter.ToCharArray(), StringSplitOptions.None);
     if (dt.Columns.Count == 0)
    {
      foreach (string str in columns)
      {  dt.Columns.Add(str); }
     continue;
     }  

    else  
    {
     DataRow dr = dt.NewRow();
     for (int i = 0; i < columns.Length;i++ )  
       {
     dr[i] = columns[i];
       }
       dt.Rows.Add(dr);
      }
    }
    }
    return dt;
    }

    ---------------------------------------------------------------------------

    Use it like this:
    ------------------------------------------------------------
    DataTable dt = new DataTable();
    dt = ImportDelimitedFile(dt,"D:\\testData.txt", " ");
    myDataGridView.DataSource=dt;

    ----------------------------------------------------------------------------------

    Add References to System.Data.Oledb, System.Data & System.IO if haven't done yet.


    Mark this as answer if it helps.
    Cheers!
    0
  • Sunny  Sharma
    0
    So you want four arrays to contain SKU. Name, Available and ShortDescription respectively or you want array of string[]?
    0
  • 0
    array of string for every string "SKU" has one array. ArrayList or List..dont know whats better for that. At the end i wanna export it to a datagriedview or Listview to see results.
    0
  • Sunny  Sharma
    0
    Hi BASCHDI,

    How about importing that CSV file directly to a DataTable and then bind this dataTable as DataSource for a DataGridView.

    Use the method below:
    -----------------------------------------------------
    static DataTable GetDataTableFromCsv(string path, bool isFirstRowHeader)
     {  

     string header = isFirstRowHeader ? "Yes" : "No";
     string pathOnly = Path.GetDirectoryName(path);  string fileName = Path.GetFileName(path);
     string sql = @"SELECT * FROM [" + fileName + "]";
     using (OleDbConnection connection = new OleDbConnection(
     @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
     ";Extended Properties=\"Text;HDR=" + header + "\""))
     using (OleDbCommand command = new OleDbCommand(sql, connection))
     using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))  {
     DataTable dataTable = new DataTable();  dataTable.Locale = CultureInfo.CurrentCulture;
     adapter.Fill(dataTable);  return dataTable;  }  }

    -----------------------------------------------------

    Use it like this:
    ---------------------------------------
    DataTable dt = GetDataTableFromCsv("D:\\test.csv", true);

    myDataGridView.DataSource=dt;
    ---------------------------------------

    As simple as that :)

    Happy Coding.

    Do mark it as answer if it helps.
    Cheers!
    0
  • 0
    Could you do the string.split with Regex.

    The File i got is not delimited with tab, just Whitespaces.

    And its a .txt , not CSV. (sry)

    I need to split the strings with regex then put the splitet strings, every one, in one seperate array.


    Like this:

    List list = new List();

    list.Add(2);
    list.Add(3);
    list.Add(5);
    list.Add(7);
    0