Skip to content

QuickCSVLiteLoad

Cinchoo edited this page Dec 30, 2021 · 2 revisions

Loading CSV file

To load CSV file, use ChoCSVLiteReader component to parse it. Sample below shows how to load CSV file (Emp.csv)

Id,Name
1,Tom
2,Carl
3,Mark

Load using iterator

using (var r = new ChoCSVLiteReader())
{
    //Open the reader, skip the header
    foreach (var cols in r.ReadFile("emp.csv").Skip(1))
    {
        Console.WriteLine($"Id: {cols[0]}");
        Console.WriteLine($"Name: {cols[1]}");
    }
}

Load using loop

using (var r = new ChoCSVLiteReader())
{
    //Open the reader, skip the header
    var recNum =  r.ReadFile("emp.csv").Skip(1).GetEnumerator();
    while (recNum.MoveNext())
    {
        var cols = recNum.Current;
        Console.WriteLine($"Id: {cols[0]}");
        Console.WriteLine($"Name: {cols[1]}");
    }
}

Load using POCO object

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}
foreach (var rec in r.ReadFile<EmployeeRec>("emp.csv", true, 
     mapper: (lineno, cols, rec) =>
     {
         rec.Id = cols[0].CastTo<int>();
         rec.Name = cols[1];
     }))
{
    Console.WriteLine($"Id: {rec.Id}");
    Console.WriteLine($"Name: {rec.Name}");
}

Please visit below article for detailed walk-through of CSV reader

Cinchoo ETL - CSV Lite Reader