我最喜欢的CSV解析器是一个内置在.NET库中的解析器。这是Microsoft.VisualBasic命名空间中隐藏的宝藏。下面是一个示例代码:using Microsoft.VisualBasic.FileIO;var path = @"C:\Person.csv";
// Habeeb, "Dubai Media City, Dubai"using (TextFieldParser csvParser = new TextFieldParser(path)){
csvParser.CommentTokens = new string[] { "#" };
csvParser.SetDelimiters(new string[] { "," });
csvParser.HasFieldsEnclosedInQuotes = true;
// Skip the row with the column names
csvParser.ReadLine();
while (!csvParser.EndOfData)
{
// Read current line fields, pointer moves to the next line.
string[] fields = csvParser.ReadFields();
string Name = fields[0];
string Address = fields[1];
}}
记住要添加引用Microsoft.VisualBasic