我目前正在为 DataTable 构建一个超级通用的 csv 阅读器。这就是我的 TextFieldParser 代码当前的样子:
using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
// Solution to HDR problem: When getting to the checkbox part of the application,
// just keep the HDR unchecked.
csvReader.SetDelimiters(new string[] { "," });
// Can be true depending on if the csv document rows are enclosed in quotes or
// not. Delimiters are set as either ; or , depending on weather the csv columns
// are split with ; or ,
csvReader.HasFieldsEnclosedInQuotes = false;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn Titel = new DataColumn(column);
Titel.AllowDBNull = true;
csvData.Columns.Add(Titel);
DataColumn LastName = new DataColumn(column);
LastName.AllowDBNull = true;
csvData.Columns.Add(LastName);
DataColumn AbstrNum = new DataColumn(column);
AbstrNum.AllowDBNull = true;
csvData.Columns.Add(AbstrNum);
DataColumn PosterTitel = new DataColumn(column);
PosterTitel.AllowDBNull = true;
csvData.Columns.Add(PosterTitel);
DataColumn Workshop = new DataColumn(column);
Workshop.AllowDBNull = true;
csvData.Columns.Add(Workshop);
DataColumn Keywords = new DataColumn(column);
Keywords.AllowDBNull = true;
csvData.Columns.Add(Keywords);
DataColumn Institution = new DataColumn(column);
Institution.AllowDBNull = true;
csvData.Columns.Add(Institution);
DataColumn CollabEmail = new DataColumn(column);
CollabEmail.AllowDBNull = true;
csvData.Columns.Add(CollabEmail);
}
...
}
但我真的很想把这部分放在一个下拉列表中或与
“;”的其他可能选择类似。和 ” ”:
csvReader.SetDelimiters(new string[] { "," });
我已将代码与其他代码部分放在控制器操作结果中,并带有一个视图,该视图采用 DataTable 的结果能够更改一些结果,然后将其添加到数据库中。
我只希望添加 csv 文件的用户能够在 TextFieldParser 中选择分隔符。
一直在寻找几个小时。
不可能的?