using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "example.csv"; // CSV文件路径
try
{
using (StreamReader reader = new StreamReader(filePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
// 按逗号分割每一行的数据
string[] values = line.Split(',');
// 输出每一列的值
foreach (string value in values)
{
Console.WriteLine(value);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("读取CSV文件时出错: " + ex.Message);
}
}
}
using System; 和 using System.IO; 导入了必要的命名空间,以便使用控制台输出和文件操作。static void Main() 是程序的入口点。string filePath = "example.csv"; 定义了要读取的CSV文件路径。StreamReader 读取文件:通过 StreamReader 类逐行读取CSV文件的内容。while 循环中,每次读取一行,并使用 Split(',') 方法将该行按逗号分割成多个字段。foreach 循环遍历并输出每个字段的值。try-catch 块捕获并处理可能发生的异常,如文件不存在或读取错误。上一篇:c#扩展方法
下一篇:c# string 转 int
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站