2017-02-17 10 views
0

FileHelper 3.1.5を使用してユーザー提供のCSVファイルを解析しようとしています。私はコンパイル時に列名を知らない。列のデータと、場合によってはヘッダー情報の両方を動的に読み取る方法が必要です。列名がわからないときにFileHelpersでCSVを解析するサンプル

これは可能ですか?すべての例が静的クラスに解析されています。

答えて

1

もちろんできます。実行時にインポート仕様を構築するにはFileHelpers class builderを使用してください。次のようなもの:

// 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"); 
関連する問題