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

こ雲淡風輕ζ 提交于 2019-12-01 10:19:23

问题


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?


回答1:


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!




回答2:


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



来源:https://stackoverflow.com/questions/4585726/readstreamasdt-filehelpers-and-c-sharp-how-do-i-dynamically-read-in-a-csv-us

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!