我要做的是让我的程序能够显示任何给定的csv文件,作为一个使用DataGrid的表(只要它是类似的显示类型,就可以是其他的东西)。主要的问题是,我没有遇到任何解决方案,不需要一个特定的类来显示数据(比如Person或某某事物)。目前,我正在尝试使用CsvHelper并从那里使用GetRecords函数,但最终得到了一个动态对象列表,无法从中提取它们的属性和值(但在调试模式下,我看到数据被正确读取)。有没有办法在不涉及太多黑客的情况下完成这样的任务?
我提到的事情:
准备好类时的https://www.pluralsight.com/guides/building-a-generic-csv-writer-reader-using-reflection
发布于 2022-05-11 20:07:00
虽然看起来很奇怪,但我知道的最简单的方法是使用Microsoft.Visual Basic包。
using Microsoft.VisualBasic.FileIO;
public static void ReadFromCSVFile()
{
using (TextFieldParser parser = new TextFieldParser(@"c:\example.csv"))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
bool first = true;
while (!parser.EndOfData)
{
string[] fields = parser.ReadFields();
if (first) { first = false; continue; } // skip the header if need
Console.WriteLine(fields[0], fields[1], fields[2]); // or add to DataGrid.ItemSource
}
}
}https://stackoverflow.com/questions/72174433
复制相似问题