using System.Reflection.Metadata;
namespace CSVImporter
{
public class Importer
{
///
/// Import CSV data to 2D List based field
///
/// Path of CSV File
/// Separator between values
/// Skip first x rows (do not load)
/// List based 2D field. Main list contains collumns and contained list values
public static List> ImportCSV(string path, string separator, int skip = 0)
{
List> Collumns = new List>(); //Initialize Lists Object
int RowsCount = 0; //Counting number of Rows
foreach (string row in File.ReadAllLines(path)) //Read Lines of CSV file
{
RowsCount++; //Count Row
if (skip < RowsCount && row.Contains(separator) && row != "") //test for skipping specified rows for skip or skip empty rows and if missing separator in row
{
List Collumn = row.Split(separator).ToList(); //Splitting CSV Line
if (Collumns.Count < Collumn.Count) //Test if sufficient number of collumns
{
int collumnsRequest = Collumn.Count - Collumns.Count; //Count missing collumns
while (collumnsRequest > 0) //loop for add missing collumns
{
Collumns.Add(new List(RowsCount - skip)); //Add collumn to collumns with specified number of elements
collumnsRequest--; //subtraction added collumn
}
}
foreach (string data in Collumn) //loop for moving data to position
{
Collumns[Collumn.IndexOf(data)].Add(data); //Adding data to position
}
if (Collumns.Count > Collumn.Count) //test if new Row is not shorter when previous
{
int missingnumber = Collumns.Count - Collumn.Count; //count missing collumns
while(missingnumber > 0) //loop for adding empty strings to collumns in shorter row
{
Collumns[Collumn.Count + missingnumber].Add(""); //adding empty strings to collumns in shorter row
missingnumber--; //subtraction number of missing collumns data
}
}
}
}
return Collumns; //return Imported Data
}
}
}