首页 >后端开发 >C++ >如何仅使用本机 .NET 读取 CSV 文件?

如何仅使用本机 .NET 读取 CSV 文件?

Linda Hamilton
Linda Hamilton原创
2025-01-03 20:24:40492浏览

How to Read a CSV File Using Only Native .NET?

如何使用本机.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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn