简体   繁体   中英

Display csv read data in console application in c#

I am completely new to programming and trying to get the complete row data from csv file based on column value in c#. Example data is as follows: Mat_No;Device;Mat_Des;Dispo_lvl;Plnt;MS;IPDS;TM;Scope;Dev_Cat 1111;BLB A601;BLB A601;T2;PW01;10;;OP_ELE;LED; 2222;ALP A0001;ALP A0001;T2;PW01;10;;OP_ELE;LED; Mat_No;Device;Mat_Des;Dispo_lvl;Plnt;MS;IPDS;TM;Scope;Dev_Cat 1111;BLB A601;BLB A601;T2;PW01;10;;OP_ELE;LED; 2222;ALP A0001;ALP A0001;T2;PW01;10;;OP_ELE;LED;

If user enters a Mat_No he gets the full row data of that particular number.
I have two files program.cs and filling.cs
overViewArea.cs contain following code for csv file reading:
I dont know how to access the read values from program.cs file and display in console

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

namespace TSDB
{
    class fillData

    {
        public static fillData readCsv()
        {
            fillData getData= new fillData ();

            using (var reader = new StreamReader(@"myfile.csv"))
            {
                List<string> headerList = null;


                while (!reader.EndOfStream)

                {
                    var line = reader.ReadLine();
                    if(headerList==null)
                    {
                        headerList = line.Split(';').ToList();
                    }
                    else
                    {
                        var values = line.Split(';');
                        for(int i = 0; i< headerList.Count; i++)
                        {
                            Console.Write(headerList[i] + "=" + values[i]+";");
                        }
                        Console.WriteLine();
                    }

                }
            }

            return fillData;
        }



    }
}`


Program.cs has following code

class Program
{
    static void Main(string[] args)
    {
        fillData data= fillData.readCsv();
        Console.ReadLine();
    }

}

First, please, do not reinvent the wheel : there are many CSV readers available: just use one of them. If you have to use your own routine (say, for a student project), I suggest extracting method . Try using File class instead of Stream / StreamReader :

// Simple: quotation has not been implemented
// Disclamer: demo only, do not use your own CSV readers
public static IEnumerable<string[]> ReadCsvSimple(string file, char delimiter) {
  return File
    .ReadLines(file)
    .Where(line => !string.IsNullOrEmpty(line)) // skip empty lines if any
    .Select(line => line.Split(delimiter));
}

Having this routine implemented, you can use Linq to query the data, eg

If user enters a Mat_No he gets the full row data of that particular number.

Console.WriteLine("Mat No, please?");  
string Mat_No_To_Filter = Console.ReadLine();

var result = ReadCsvSimple(@"myfile.csv", ';')
  .Skip(1)
  .Where(record => record[0] == Mat_No_To_Filter);

foreach (var items in result) 
  Console.WriteLine(string.Join(";", items));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM