70行の.csvファイルがあるとしますが、必要なのは5行だけです。私は、私が望む列名の文字列配列をメソッドに渡すことができ、それがデータテーブルを返すことができるようにしたい。あなたはhttps://joshclose.github.io/CsvHelper/列名の文字列配列を使用して、.csvファイルをDataTableに読み込むにはどうすればよいですか?
で見ることができ
private void method(object sender, EventArgs e) {
string[] columns =
{
@"Column21",
@"Column48"
};
DataTable myDataTable = Get_DT(columns);
}
public DataTable Get_DT(string[] columns) {
DataTable ret = new DataTable();
if (columns.Length > 0)
{
foreach (string column in columns)
{
ret.Columns.Add(column);
}
string[] csvlines = File.ReadAllLines(@"path to csv file");
csvlines = csvlines.Skip(1).ToArray(); //ignore the columns in the first line of the csv file
//this is where i need help... i want to use linq to read the fields
//of the each row with only the columns name given in the string[]
//named columns
}
return ret;
}
を通してあなたを取得する必要があります - あなたはOLEDBを使用してインポート&だけで選択することができますあなたが望む列。 https://stackoverflow.com/questions/6813607/parsing-csv-using-oledb-using-c-sharp – PaulF
素晴らしいアイデアのようです@PaulF。 [例](https://stackoverflow.com/questions/6813607/parsing-csv-using-oledb-using-c-sharp)です。 –
CSVにヘッダファイルがない場合は、同じフォルダにschema.iniファイルを指定してフィールド名とタイプを指定することができます。 https://docs.microsoft.com/en-us/sql/odbc/microsoft/schema-ini-file-text-file-driver – PaulF