c#CSVHelper读取带有可变标题的CSV
第一次使用csvReader-注意,它需要一个自定义类来定义CSV文件中的头c#CSVHelper读取带有可变标题的CSV,c#,winforms,csv,csvhelper,C#,Winforms,Csv,Csvhelper,第一次使用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; }...... } 然后给出的示例使用以下类:-
class DataRecord
{
//Should have properties which correspond to the Column Names in the file
public String Amount { get; set; }
public String InvoiceDate { get; set; }......
}
然后给出的示例使用以下类:-
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, ....);
}
使用(var sr=new StreamReader(@“C:\\Data\\Invoices.csv”))
{
var读取器=新的CsvReader(sr);
//CSVReader现在将整个文件读入一个可枚举文件
IEnumerable records=reader.GetRecords();
//CSV文件中的前5条记录将打印到输出窗口
foreach(记录中的DataRecord记录。Take(5))
{
Print(“{0}{1},{2}”,record.Amount,record.InvoiceDate,…);
}
两个问题:-
1.应用程序将加载具有不同标题的文件,因此我需要能够动态更新该类-这可能吗?如何实现?
(我能够从CSV文件中提取标题。)
尽管使用reflecion或第三方库是完全可能的,但是为一行创建对象对于如此大的文件来说效率很低。此外,在这种情况下使用C#是一个坏主意(除非您进行了一些业务数据转换)我会考虑一些类似的,或者是SSIS包。我已经使用了LIQ2CSV,但不知道它是如何缩放到数据的GB。它最好把它分解成块。它也允许你即时定义标题。这很好。看起来不错,就像文档一样。谢谢你强调我没有看到这个选项。粉笔。it-to-it文盲终端用户:(我必须导入大型金融数据集,对数据执行统计例程/数据聚合/关键字搜索等,然后制作一份报告。我尝试过各种方法,但厌倦了手持,尽管将其制作成应用程序形式将有望使其成为用户的最佳证明(如果我能正确编码!!)您可以使用OleDbConnection“连接”到csv文件,然后用常规SQL语句查询它。请参阅。我希望这会有所帮助