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

后端 未结 2 1134
闹比i
闹比i 2021-01-15 16:02

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

相关标签:
2条回答
  • 2021-01-15 16:34

    Have you tried using http://www.codeproject.com/KB/database/CsvReader.aspx ? You could leverage this library's parsing. It's fast and reliable.

    0 讨论(0)
  • 2021-01-15 17:00

    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!

    0 讨论(0)
提交回复
热议问题