[c]
DataTable dt = new DataTable();
using (var stream = File.OpenRead(filePath))
{
using (var reader = new StreamReader(stream))
{
using (var csvReader = new CsvReader(reader))
{
int i = 0;
while (csvReader.Read())
{
if (i == 0)
{
foreach (var field in csvReader.FieldHeaders)
{
dt.Columns.Add(field);
}
}
DataRow row = dt.NewRow();
foreach (var field in csvReader.FieldHeaders)
{
row[field] = csvReader.GetField(field);
}
dt.Rows.Add(row);
i += 1;
}
}
}
}
return dt;
[/c]
Discover more from naiwaen@DebuggingSoft
Subscribe to get the latest posts sent to your email.