Hello
I am trying to convert manually a CSV file into a datatable for posterior analysis and filtering.
The company was doing it manually in Excel but I’d figure it can be done programmatically and increase productivity.
I did try do use some existing classes and nuggets without success (Errors I can’t fix or don’t understand).
Could someone please look at my code and help me figure how to convert manually a CSV with '|' as divider between cells to datatable and apply filtering please. Or Just point me in the right direction.
I am trying this: https://www.c-sharpcorner.com/forums/c-sharp-importing-csv-files-into-datatables
I like to figure things by myself but this is taking me forever.
Best regards
Rui
- private void ConvertCSVtoDataTable(string filePath)
- {
- LogMessage("(<-o->)");
-
-
- long lineCount = 0;
- int columnsCount = 0;
- char divider = '|';
-
-
- string firstLine = File.ReadLines(filePath).First();
-
-
- foreach (char c in firstLine) if (c == divider) columnsCount++;
-
-
-
- DataTable dt = new DataTable();
- dt.Clear();
- dt.Columns.Add("a");
- dt.Columns.Add("b");
- dt.Columns.Add("c");
- dt.Columns.Add("d");
- dt.Columns.Add("e");
- dt.Columns.Add("f");
- dt.Columns.Add("g");
- dt.Columns.Add("h");
- dt.Columns.Add("i");
- dt.Columns.Add("j");
- dt.Columns.Add("k");
- dt.Columns.Add("l");
- dt.Columns.Add("m");
- dt.Columns.Add("n");
- dt.Columns.Add("o");
- dt.Columns.Add("p");
- dt.Columns.Add("q");
- dt.Columns.Add("r");
- dt.Columns.Add("s");
- dt.Columns.Add("t");
- dt.Columns.Add("u");
- dt.Columns.Add("v");
- dt.Columns.Add("w");
- dt.Columns.Add("x");
- dt.Columns.Add("y");
- dt.Columns.Add("z");
-
-
-
- using (StreamReader r = new StreamReader(filePath))
- {
- string line;
-
-
- while ((line = r.ReadLine()) != null)
- {
-
- while (line.IndexOf(divider) != -1)
- {
-
-
-
- int endBlock = line.IndexOf(divider);
-
-
- String firstBlock = line.Substring(0, endBlock);
- LogMessage("Line: " + lineCount + " - Block: " + firstBlock);
-
- line = line.Remove(0, firstBlock.Length + 1);
-
-
-
-
-
- lineCount++;
- }
- }
- }
-
- }