Přidejte soubory projektu.

master
jan00 2023-10-28 09:05:07 +02:00
parent 42128bf470
commit f3b2d17b34
2 changed files with 98 additions and 0 deletions

9
CSVImporter.csproj Normal file
View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

89
Importer.cs Normal file
View File

@ -0,0 +1,89 @@
using System.Reflection.Metadata;
namespace CSVImporter
{
public class Importer
{
/// <summary>
/// Import CSV data to 2D List pole
/// </summary>
/// <param name="path">Path of CSV File</param>
/// <param name="separator">Separator between values</param>
/// <param name="skip">Skip first x rows (do not load)</param>
/// <returns></returns>
public static List<List<string>> ImportCSV(string path, string separator, int skip = 0)
{
List<List<string>> Collumns = new List<List<string>>(); //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<string> 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<string>(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
}
}
}