从CSV读取数据行然后显示数据

问题描述 投票:0回答:1

我必须从txt文件中读取信息,以某种方式(数组或列表)进行存储,然后显示数据。程序必须至少包含一个附加类。我撞墙了,无法前进。

字符串,字符串,双精度字符串,字符串名称,徽章,工资,职位名称,徽章,工资,职位名称,徽章,薪水,职位

对不起,我知道下面的代码是灾难性的,但是我很茫然,时间用完了。

namespace Employees
{
    class Program
    {
        static void Main()
        {
            IndividualInfo collect = new IndividualInfo();

            greeting();
            collect.ReadInfo();
            next();
            for (int i = 0; i < 5; i++)
            {
                displayInfo(i);
            }
            exit();

            void greeting()
            {
                Console.WriteLine("\nWelcome to the Software Development Company\n");
            }
            void next()
            {
                Console.WriteLine("\n*Press enter key to display information . . . *");
                Console.Read();
            }
            void displayInfo(int i)
            {
                Console.WriteLine($"\nSoftware Developer {i + 1} Information:");
                Console.WriteLine($"\nName:\t\t\t{collect.nameList[i]}");
            }
            void exit()
            {
                Console.WriteLine("\n\n*Press enter key to exit . . . *");
                Console.Read();
                Console.Read();
            }
        }
    }
}

class IndividualInfo
    {
        public string Name { get; set; }
        //public string Badge{ get; set; }
        //public string Position{ get; set; }
        //public string Salary{ get; set; }

        public void ReadInfo()
        {
            int i = 0;
            string inputLine;
            string[] eachLine = new string[4];
            string[,] info = new string[5, 4]; // 5 developers, 4x info each
            StreamReader file = new StreamReader("data.txt");
            while ((inputLine = file.ReadLine()) != null)
            {
                eachLine = inputLine.Split(',');
                for (int x = 0; x < 5; x++)
                {
                    eachLine[x] = info[i, x];
                    x++;
                }
                i++;
            }
            string name = info[i, 0];
            string badge = info[i, 1];
            string position = info[i, 2];
            double salary = Double.Parse(info[i, 3]);
        }

        public List<string> nameList = new List<string>();
    }

到目前为止,我想我可以用二维数组来收集它,但是最好使用一个List。另外,我在那里发布的代码将无法运行,因为我仍无法找到一种方法来显示它。这就是为什么我在这里。

c# readfile
1个回答
0
投票
using System.IO;

static void Main(string[] args)
{
    using(var reader = new StreamReader(@"C:\test.csv"))
    {
        List<string> listA = new List<string>();
        List<string> listB = new List<string>();
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var values = line.Split(';');

            listA.Add(values[0]);
            listB.Add(values[1]);
        }
    }
}

https://tools.ietf.org/html/rfc4180

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];
 }
}

http://codeskaters.blogspot.ae/2015/11/c-easiest-csv-parser-built-in-net.html

© www.soinside.com 2019 - 2024. All rights reserved.