>  기사  >  백엔드 개발  >  C# CSV 리더

C# CSV 리더

王林
王林원래의
2024-09-03 15:20:51851검색

A simple C# class library which is lightweight and open source that can read the data in CSV format from text files and strings is called CSV Reader in C# whose latest version must be downloaded and included in the project we are creating as a compiled DLL or as a source and it builds itself in all the versions of Microsoft visual studio 2008 or CSVReader.cs and StringConverter.cs can be downloaded to include the CSV Reader directly in our source code which can be added to the C# project we are doing on Microsoft Visual Studio and CSV Reader class belongs to DataSreams.csv namespace.

The syntax of C# CSV Reader is as follows:

public sealed class CsvReader : ReaderBase

Working of C# CSV Reader

  • Whenever there is a need to read the comma-separated values file through a C# program, we make use of CSV Reader.
  • CSV Reader belongs to DataStrems.csv namespace.
  • CSV Reader is a simple library that is lightweight and open source that can read the data in CSV format from text files and strings.
  • The latest version of CSV Reader can be downloaded from Microsoft visual studio 2008 and must be included in the project we are creating as a compiled DLL or as a source.
  • The other way to include the CVS Reader into the source code is to download the CSVReader.cs and StringConverter.cs and add to the project we are creating on Microsoft Visual Studio.

Examples of C# CSV Reader

Here are the following examples mention below

Example #1

Program to demonstrate CSV Reader in a program to read the contents of a CSV file

Code:

using System;
using System.Diagnostics;
using System.IO;
using Microsoft.VisualBasic.FileIO;
//a class called check is defined
class check
{
//an array of strings is defined to store the values from the csv file as an array
List<string[]> records = new List<string[]>();
//an instance of csv reader class is created by giving the path of the csv file
using (CsvReader reader = new CsvReader("C:\\Users\\admin\\Desktop\\imp.csv", Encoding.Default))
{
//the csv file is read until the last record is reached by making use of fields’ property
while (reader.ReadNextRecord())
//adding the records from the read csv file to the array
records.Add(reader.Fields);
}
//displaying the contents of the array
foreach (var array in records)
{
Console.WriteLine("{0}", array);
}
}

Output:

C# CSV 리더

In the above program, a class called check is defined. Then an array of strings is defined to store the values from the csv file as an array. Then an instance of csv reader class is created by giving the path of the csv file. Then the records are read from the csv file until the last record is reached by making use of fields’ property and then the read records are added to the array by using the Add method. Then the contents of the array is displayed. The output is as shown above.

Note: Please save the input file in .csv format before providing the path of the file as input to the program.

Example #2

Program to demonstrate CSV Reader in a program to read the contents of a CSV file

Code:

using System;
using System.Diagnostics;
using System.IO;
using Microsoft.VisualBasic.FileIO;
//a class called check is defined
class check
{
//an array of integers is defined to store the values from the csv file as an array
List<int[]> records = new List<int[]>();
//an instance of csv reader class is created by giving the path of the csv file
using (CsvReader reader = new CsvReader("C:\\Users\\admin\\Desktop\\imp1.csv", Encoding.Default))
{
//the csv file is read until the last record is reached by making use of fields’ property
while (reader.ReadNextRecord())
//adding the records from the read csv file to the array
records.Add(reader.Fields);
}
//displaying the contents of the array
foreach (var array in records)
{
Console.WriteLine("{0}", array);
}
}

Output:

C# CSV 리더

In the above program, a class called check is defined. Then an array of strings is defined to store the values from the csv file as an array. Then an instance of csv reader class is created by giving the path of the csv file. Then the records are read from the csv file until the last record is reached by making use of fields’ property and then the read records are added to the array by using the Add method. Then the contents of the array is displayed. The output is as shown above.

Note: Please save the input file in .csv format before providing the path of the file as input to the program.

Advantages

There are several advantages of using CSV Reader. They are:

  • Making use of CSV Reader in our program is the easiest way to read the files in CSV format.
  • The performance of the program while reading the file in CSV format by making use of CSV Reader in our program is really good.
  • CSV Reader used in our program follows the file format of a csv file completely making the reading task as easier as possible.
  • There is no necessity to make use of third party libraries to read a file in csv format if we are making use of CSV Reader in our program.

Conclusion

In this tutorial, we understand the concept of CSV Reader in C# through definition, syntax, and working of CSV Reader through programming examples and their outputs and the advantages of using CSV Reader in our program to read a file in CSV format.

위 내용은 C# CSV 리더의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:C#의 예외 유형다음 기사:C#의 예외 유형