【发布时间】:2015-03-15 16:45:25
【问题描述】:
第一次使用 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; }......
}
然后给出的示例使用这样的类:-
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, ....);
}
两个问题:- 1. 该应用程序将加载具有不同标题的文件,因此我需要能够即时更新此类 - 这可能吗?如何? (我可以从 CSV 文件中提取标题。)
- CSV 文件可能有数百万行(gb 大小),因此这是导入文件的最佳/最有效方式。
目标是一个 SQLite DB - 以调试行为例。
谢谢
【问题讨论】:
-
我使用 linq2csv 取得了巨大的成功。不确定它如何扩展到 GB 的数据。最好把它分成块。codeproject.com/Articles/25133/LINQ-to-CSV-library
-
它也允许您动态定义标题,这很好。
-
看起来不错,像文档一样 - 感谢您突出显示,因为我没有看到这个选项。