Search code examples
csvreflectionfilehelperscsvhelper

CSV Parser, how to auto discover col names & types


I am loading CVS files for data import. The files come from various sources so the header names and location are often changing. I searched and found helpful libs like CsvHelper & FileHelpers

Question: either using FileHelper.net or CsvHelper, how do we extract both the Header names & the Column datatype? so that I can create a drop for each col, to map between .NET type <==> to a SQL type


Solution

  • Just read in the first line of the file with, say,

    string headers = File.ReadLines("MyFile.txt").First();
    

    And then use a class builder to build whatever CSV spec you need.

    DelimitedClassBuilder cb = new DelimitedClassBuilder("MyProduct", delimiter: ",");
    
    cb.AddField("Name", typeof(string));
    cb.LastField.TrimMode = TrimMode.Both;
    
    cb.AddField("Description", typeof(string));
    cb.LastField.FieldQuoted = true;
    cb.LastField.QuoteChar = '"';
    cb.LastField.QuoteMode = QuoteMode.OptionalForBoth;
    
    // etc... e.g., add a date field
    cb.AddField("SomeDate", typeof(DateTime));
    
    engine = new FileHelperEngine(cb.CreateRecordClass());
    
    DataTable dt = engine.ReadFileAsDT("test.txt");