简体   繁体   English

c#CSVHelper使用可变标题读取CSV

[英]c# CSVHelper read CSV with variable headers

First time using the csvReader - note it requires a custom class that defines the Headers found in the CSV file. 首次使用csvReader-请注意,它需要一个自定义类,该类定义在CSV文件中找到的标头。

class DataRecord
{
    //Should have properties which correspond to the Column Names in the file 
    public String Amount { get; set; }
    public String InvoiceDate { get; set; }......
}

The example given then uses the class such:- 然后,给出的示例使用此类:

            using (var sr = new StreamReader(@"C:\\Data\\Invoices.csv"))
        {
            var reader = new CsvReader(sr);

            //CSVReader will now read the whole file into an enumerable
            IEnumerable<DataRecord> records = reader.GetRecords<DataRecord>();

            //First 5 records in CSV file will be printed to the Output Window
            foreach (DataRecord record in records.Take(5)) 
            {
                Debug.Print("{0} {1}, {2}", record.Amount, record.InvoiceDate, ....);
            }

Two questions :- 1. The app will be loading in files with differing headers so I need to be able to update this class on the fly - is this possible & how? 两个问题:1.该应用程序将加载具有不同标题的文件,因此我需要能够动态更新此类-这可能吗? (I am able to extract the headers from the CSV file.) (我能够从CSV文件中提取标题。)

  1. CSV file is potentially multi millions of rows (gb size) so is this the best / most efficient way of importing the file. CSV文件可能包含数百万行(GB大小),因此这是导入文件的最佳/最有效方法。

Destination is a SQLite DB - debug line is used as example. 目标是一个SQLite数据库-以调试行为例。

Thanks 谢谢

  1. The app will be loading in files with differing headers so I need to be able to update this class on the fly - is this possible & how? 该应用程序将加载具有不同标题的文件,因此我需要能够即时更新此类-这可能吗?

Although it is definetely possible with reflecion or third part libraries, creating an object for a row will be inefficient for such a big files. 尽管使用反射库或第三方库绝对有可能,但是为这样的大文件创建行对象将效率很低。 Moreover, using C# for such a scenario is a bad idea (unless you have some business data transformation). 此外,在这种情况下使用C#是个坏主意(除非您进行了一些业务数据转换)。 I would consider something like this , or perhaps a SSIS package. 我认为像这样 ,或者一个SSIS包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM