CSV file using a column Csvhelper read a row of data

the System the using;
the using the System.Collections.Generic;
the using the System.IO;
the using the System.Linq;
the using the System.Text;
the using System.Threading.Tasks;
the using CsvHelper;
the using CsvHelper.Configuration;

namespace ConsoleApplication5
{
    class Program
    {
        static void the Main ( String [] args)
        {
            // detect the original document read in encoded form, or default

            StreamReader SRFile = new StreamReader ( "C : \\ Users \\ XXX \\ Desktop \\ Rfid.csv", Encoding.Default);
            CSVReader new new CSV = var (SRFile);

            csv.Configuration.RegisterClassMap <RfidMap> (); // configuration matching class

            // data are mapped to Rfid
            
            var records = csv.GetRecords <Rfid> ( ) ToList ();.
            value // output of the read csv
            int n-=. 1;
            the foreach (Records in R & lt var)
            {
              
                IF (n-== 2) // Sets reading the line; second row, for example,
                {
                    String DATE = r.NUM; // get the field directly
                    Console.WriteLine (DATE);
                    BREAK; // out of the loop
                }
                n-= ++;
            }
            SRFile.Close (); / / file Close
            the Console.ReadKey ();
        }
    }
    file // class name (Rfid) requires the same name and csv
    public class Rfid
    {
        // field names and header names csv need of same
        NUM GET String {public; SET;}
        public String {DATE GET; SET;}
        public String the RFID {GET; SET;}
    }
    // if the field named Chinese, you can define a mapping class matches, CSVHelper automatically based on your class to automatically map matching
    public class RfidMap Sealed: ClassMap <Rfid>
    {
        RfidMap ()
        {
            map (m => m.NUM) .Name ( "No."); // name specified using the file list map
            map (m = > m.DATE) .Name ( "time");
            the Map (m => m.RFID) .Name ( "the RFID tag the UID");
        }
    }

}
may need to download libraries csvhelper; tools in VS - library package manager - package Manager console, enter the install-package CsvHelper, if an error can not be installed, it may be the wrong version, finding the right version https://www.nuget.org/packages/CsvHelper/, as install-package CsvHelper -Version 9.0.0 can be.

Guess you like

Origin www.cnblogs.com/fuzhuoyicheng/p/11982219.html