Jadual Kandungan
Working of C# CSV Reader
Examples of C# CSV Reader
Example #2
Advantages
Conclusion

Pembaca C# CSV

Sep 03, 2024 pm 03:20 PM
c# c# tutorial

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
Salin selepas log masuk

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);
}
}
Salin selepas log masuk

Output:

Pembaca 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);
}
}
Salin selepas log masuk

Output:

Pembaca 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.

Atas ialah kandungan terperinci Pembaca C# CSV. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

Video Face Swap

Video Face Swap

Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

<🎜>: Bubble Gum Simulator Infinity - Cara Mendapatkan dan Menggunakan Kekunci Diraja
4 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Sistem Fusion, dijelaskan
4 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers of the Witch Tree - Cara Membuka Kunci Cangkuk Bergelut
3 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas

Tutorial Java
1670
14
Tutorial PHP
1276
29
Tutorial C#
1256
24
Direktori Aktif dengan C# Direktori Aktif dengan C# Sep 03, 2024 pm 03:33 PM

Panduan untuk Active Directory dengan C#. Di sini kita membincangkan pengenalan dan cara Active Directory berfungsi dalam C# bersama-sama dengan sintaks dan contoh.

Penjana Nombor Rawak dalam C# Penjana Nombor Rawak dalam C# Sep 03, 2024 pm 03:34 PM

Panduan untuk Penjana Nombor Rawak dalam C#. Di sini kita membincangkan cara Penjana Nombor Rawak berfungsi, konsep nombor pseudo-rawak dan selamat.

Paparan Grid Data C# Paparan Grid Data C# Sep 03, 2024 pm 03:32 PM

Panduan untuk Paparan Grid Data C#. Di sini kita membincangkan contoh cara paparan grid data boleh dimuatkan dan dieksport daripada pangkalan data SQL atau fail excel.

Faktorial dalam C# Faktorial dalam C# Sep 03, 2024 pm 03:34 PM

Panduan untuk Faktorial dalam C#. Di sini kita membincangkan pengenalan kepada faktorial dalam c# bersama-sama dengan contoh dan pelaksanaan kod yang berbeza.

Perbezaan antara multithreading dan asynchronous C# Perbezaan antara multithreading dan asynchronous C# Apr 03, 2025 pm 02:57 PM

Perbezaan antara multithreading dan asynchronous adalah bahawa multithreading melaksanakan pelbagai benang pada masa yang sama, sementara secara tidak sengaja melakukan operasi tanpa menyekat benang semasa. Multithreading digunakan untuk tugas-tugas yang berintensifkan, sementara asynchronously digunakan untuk interaksi pengguna. Kelebihan multi-threading adalah untuk meningkatkan prestasi pengkomputeran, sementara kelebihan asynchronous adalah untuk tidak menghalang benang UI. Memilih multithreading atau asynchronous bergantung kepada sifat tugas: tugas-tugas intensif pengiraan menggunakan multithreading, tugas yang berinteraksi dengan sumber luaran dan perlu menyimpan respons UI menggunakan asynchronous.

Corak dalam C# Corak dalam C# Sep 03, 2024 pm 03:33 PM

Panduan kepada Corak dalam C#. Di sini kita membincangkan pengenalan dan 3 jenis Corak teratas dalam C# bersama-sama dengan contoh dan pelaksanaan kodnya.

Nombor Perdana dalam C# Nombor Perdana dalam C# Sep 03, 2024 pm 03:35 PM

Panduan Nombor Perdana dalam C#. Di sini kita membincangkan pengenalan dan contoh nombor perdana dalam c# bersama dengan pelaksanaan kod.

Cara menukar format XML Cara menukar format XML Apr 03, 2025 am 08:42 AM

Terdapat beberapa cara untuk mengubah format XML: mengedit secara manual dengan editor teks seperti Notepad; Memformat secara automatik dengan alat pemformatan XML dalam talian atau desktop seperti XMLBeAutifier; Tentukan peraturan penukaran menggunakan alat penukaran XML seperti XSLT; atau menghuraikan dan beroperasi menggunakan bahasa pengaturcaraan seperti Python. Berhati -hati apabila mengubahsuai dan membuat sandaran fail asal.

See all articles