Search code examples
asp.netcsvhelper

CSV Helper first row been skipped


I am using CSV Helper to read a CSV file but the first row is been skipped, I saw that it can be a configuration but I can't see how to force the reading of the first row.

Any ideas?

try
{
    using var csv = new CsvReader(file);
    var records = csv.GetRecords<TMap>().ToList();
    return _mapper.Map<List<T>>(records.ToList());
}
catch (Exception e)
{
    throw new Exception($"Error parsing the Csv File. Error: {e.Message}");
}

Solution

  • you need to set the configuration first.

    Try this

    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        HasHeaderRecord = true,
    };
    try
    {
    
        using var csv = new CsvReader(file, config);
    
        var records = csv.GetRecords<TMap>().ToList();
        return _mapper.Map<List<T>>(records.ToList());
    }
    catch (Exception e)
    {
        throw new Exception($"Error parsing the Csv File. Error: {e.Message}");
    }