以下是使用C#获取CSV文件中数据的代码示例:

using System;
using System.Collections.Generic;
using System.IO;

namespace ReadCSVFile
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"C:\Users\username\Desktop\data.csv"; // CSV文件的路径

            List<string[]> data = new List<string[]>(); // 用于保存CSV文件中的数据

            using (StreamReader reader = new StreamReader(filePath))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    string[] values = line.Split(','); // 假设CSV文件中每行数据用逗号分隔

                    data.Add(values);
                }
            }

            // 输出读取到的数据
            foreach (string[] row in data)
            {
                foreach (string value in row)
                {
                    Console.Write(value + "\t");
                }
                Console.WriteLine();
            }
        }
    }
}

这段代码将CSV文件中的每行数据读取到一个string数组中,并将这些数组保存到一个List中。在这个示例中,我们假设CSV文件中每行数据用逗号分隔。如果你的CSV文件中使用不同的分隔符,你需要相应地修改代码。最后,我们将读取到的数据输出到控制台

C#获取CSV文件中的数据

原文地址: https://www.cveoy.top/t/topic/hgim 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录