簡體   English   中英

當我不知道列名時,使用FileHelpers解析CSV的示例

[英]sample of parsing CSV w/ FileHelpers when I don't know column names

我正在嘗試使用FileHelper 3.1.5來分析用戶提供的CSV文件。 我在編譯時不知道列名。 我需要一種動態讀取列的方法,包括列數據和標題信息。

那可行嗎? 所有示例都解析為靜態類。

當然可以。 使用FileHelpers類構建器在運行時構建導入規范。 就像是:

// create a FileHelpers class with a comma delimiter.
DelimitedClassBuilder cb = new DelimitedClassBuilder("Person", ",");
// add your fields based on whatever logic you like (e.g., maybe read the column names from the first row)
cb.AddField("firstName", typeof(string));
cb.AddField("lastName", typeof(string));
cb.LastField.FieldNullValue = "default last name";

// create your import engine from the class you created above.
DelimitedFileEngine engine = new DelimitedFileEngine(cb.CreateRecordClass());
DataTable dt = engine.ReadFileAsDT("data.csv"); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM