Search code examples
c#filehelpers

ReadStreamAsDT - Filehelpers and C# - How do I dynamically read in a CSV using filehelpers?


I am attempting to read in a CSV dynamically via FileHelpers and work with the CSV data as a datatable. My CSV files will not be the same. They will have different column headers and different amounts of columns. I am using the ReadStreamAsDT method, but it seems to still want a structured class to initialize the FileHelperEngine. Any ideas?


Solution

  • I had to use FileHelpers.RunTime and the DelimitedClassBuilder to create a DataTable from the file. Here is my method. If I get more time, I will explain this better.

    private static DataTable CreateDataTableFromFile(byte[] importFile) {
        var cb = new DelimitedClassBuilder("temp", ",") { IgnoreFirstLines = 0, IgnoreEmptyLines = true, Delimiter = "," };
        var ms = new MemoryStream(importFile); 
        var sr = new StreamReader(ms); 
        var headerArray = sr.ReadLine().Split(',');
        foreach (var header in headerArray) { 
            cb.AddField(header, typeof(string)); 
            cb.LastField.FieldQuoted = true; 
            cb.LastField.QuoteChar = '"'; 
        }
        var engine = new FileHelperEngine(cb.CreateRecordClass());
        return engine.ReadStreamAsDT(sr);
    }
    

    Obviously, there is a lot of validation along with other logic taking place around this method, but I do not have much time right now to dive into it. Hope this helps!