首页 > 解决方案 > 将数据从 csv 文件写入并将其转换为数组以使用数据 c#

问题描述

我试图继续我的学校项目。

我能够在命令行中从 CSV 文件导入数据。我真的不完全确定我是否已经将它写入数组。但是对于下一步,我需要继续从书面课程中过滤数据。作为第一个示例,我尝试从类中过滤日期(基准)上的书面数据。

我真的不知道现在该做什么工作。所以在这个部分:

 if (decideTyp == "Z")
            {
                Console.WriteLine("Decide to filtering date");
                var inputWert = Console.ReadLine();
                Console.WriteLine("Filtering the date and show the date: ");
                
                //Console.WriteLine($" {eineAktie.Aktienkurs_datum} {eineAktie.Aktien_id} {eineAktie.Name} {eineAktie.Wert}" /*+ eineAktie.Aktienkurs_datum*/);
                //Console.WriteLine($" {zweiteAktie.Aktienkurs_datum} {zweiteAktie.Aktien_id} {zweiteAktie.Name} {zweiteAktie.Wert}" /*+ eineAktie.Aktienkurs_datum*/);

从我的代码中,我尝试给出 CSV 文件中的所有日期。

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace aktienportfolio
{
    public class Aktie
    {
        public int Id { get; set; }
        public string Datum { get; set; }
        public string Markt { get; set; }
        public string Symbol { get; set; }
        public string Name { get; set; }
        public double Wert { get; set; }
    }

    class Program
    {
        public static void Main()
        {
            Console.WriteLine("AktienProgramm: ");
            Console.WriteLine("--------------- ");
            Console.WriteLine("\n");

            string firstDecide; //StockExchange or Portfolio

            do
            {
                Console.WriteLine("Do you like take a look at (A)ktienKurse or (P)ortfolio abfragen? \n");
                firstDecide = Console.ReadKey(true).KeyChar.ToString().ToUpper();      //Kein Casesensitiv mit ToLower.
                Console.WriteLine(firstDecide);
            }
            while (firstDecide != "A" && firstDecide != "P");     

            if (firstDecide == "A")
            {
                Console.WriteLine("\nDo you choose StockExchange, please push enter and the data will loaded adnd shown! \n");
                var inputWert = Console.ReadLine();
                Console.WriteLine("\nhere you see your data\n");

                //Read File
                string path = @"C:\Auftrag\Aktienkurse.csv";
                string[] readText = File.ReadAllLines(path);
                foreach (string s in readText)
                {
                    Console.WriteLine(s);
                }
            }
            else
            {
                Console.WriteLine("\nyou choose Portfolio, push Enter and the data will be loaded and shown! \n");
                var inputWert = Console.ReadLine();
                Console.WriteLine("\nhere you see your portfolio \n");

                //read File
                string path = @"C:\Auftrag\Portfolio.csv";
                string[] readText = File.ReadAllLines(path);

                foreach (string s in readText)
                {
                    Console.WriteLine(s);
                }
            }

            //Progamm proceed
            string decideTyp; //Question what the user like to do now
            {
                Console.WriteLine("\nDo you like filter the (Z)data through the date or the (I)content  ");
                entscheidungsTyp = Console.ReadKey(true).KeyChar.ToString().ToUpper(); 
                Console.WriteLine(entscheidungsTyp);
            }
            while (decideTyp != "Z" && decideTyp != "I"); 

            if (decideTyp == "Z")
            {
                Console.WriteLine("you've chosen the date filtering ");
                var inputWert = Console.ReadLine();
                Console.WriteLine("Show the data date: ");
            }
            else
            {
                Console.WriteLine("you decided to show the content");
                var inputWert = Console.ReadLine();
                Console.WriteLine("show the content: ");
            }

            Console.ReadKey(false);
        }
    }
}

我真的是一个初学者,所以请原谅我这些愚蠢的问题。我感谢任何帮助或建议继续我的学校项目。

以下是 CSV 文件:

活动课程

Id ";" Datum ";" Markt ";" Symbol ";" Name ";" Wert
1;2021-01-10;DowJones;BA;Boeing;135.20
2;2021-01-10;Nasdaq;MSFT;Microsoft;165.13
3;2021-01-10;Nasdaq;AMD;Advanced Micro Devices;48.79
4;2021-01-10;SMI;ABB;ASEA BROWN BOVERI;17.20
5;2021-01-10;DAX;BMW;Bayerische Motoren Werke AG;49.10
6;2021-01-11;Nasdaq;MSFT;Microsoft;166.13

文件夹

Id ";" Datum ";" Symbol ";" Anzahl
1;2021-01-10;MSFT;45
2;2021-01-6;AMD;23
3;2021-01-7;BMW;10
4;2021-01-5;AMD;30

标签: c#

解决方案


我只是重构了一个处理 CSV 的项目,但我不能重构所有东西来假装它是你的 CSV,所以你可以以此为灵感尝试这样的事情:

public class CSVModel
    {
        public string FirstRow { get; set; }
        public string SecondRow { get; set; }
        public string ThirdRow { get; set; }

    }

    private void ImportFromCSV()
    {

      

        var csv = new CSVModel();

        var csvFile = File.ReadAllLines(txtPath.Text);

         //let's say it's separated by comma
        var file = csvFile[i].Split(',');
        for (int i = 0; i < csvFile.Count(); i++)
        {
            csv.FirstRow = file[i];
            csv.SecondRow = file[i];
            csv.ThirdRow = file[i];
//PS: All properties will have the same value, this isn't the exact solution, 
//just a enlightening

}

// 这里你可以随意使用数据

}


推荐阅读