如何使用本机.NET 功能读取 CSV 文件
问题:
如何我可以使用 C# 读取 CSV 文件而不依赖第三方吗
答案:
.NET Framework 提供了 Microsoft.VisualBasic.FileIO.TextFieldParser 类来解析 CSV 文件。要使用此类,请导入 Microsoft.VisualBasic 程序集。
以下是如何使用 TextFieldParser 读取 CSV 文件:
using Microsoft.VisualBasic.FileIO; var parser = new TextFieldParser(file); parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(new string[] { ";" }); while (!parser.EndOfData) { string[] row = parser.ReadFields(); // Process the current row }
ReadFields 方法返回表示值的字符串数组CSV 文件中当前行的位置。
以上是如何仅使用本机 .NET 读取 CSV 文件?的详细内容。更多信息请关注PHP中文网其他相关文章!