自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

.NET使用CsvHelper快速讀取和寫(xiě)入CSV文件

開(kāi)發(fā) 前端
.csv 文件是一種用于存儲(chǔ)表格數(shù)據(jù)的文本文件,CSV 是 "Comma-Separated Values" 的縮寫(xiě),意思是 "逗號(hào)分隔值"。CSV 文件是一個(gè)存儲(chǔ)表格和電子表格信息的純文本文件,其內(nèi)容通常是一個(gè)文本、數(shù)字或日期的表格。CSV 文件可以使用以表格形式存儲(chǔ)數(shù)據(jù)的程序輕松導(dǎo)入和導(dǎo)出。

前言

在日常開(kāi)發(fā)中使用CSV文件進(jìn)行數(shù)據(jù)導(dǎo)入和導(dǎo)出、數(shù)據(jù)交換是非常常見(jiàn)的需求,今天我們來(lái)講講在.NET中如何使用CsvHelper這個(gè)開(kāi)源庫(kù)快速實(shí)現(xiàn)CSV文件讀取和寫(xiě)入。

CsvHelper類庫(kù)介紹

CsvHelper是一個(gè).NET開(kāi)源、快速、靈活、高度可配置、易于使用的用于讀取和寫(xiě)入CSV文件的類庫(kù)。

圖片圖片

CsvHelper類庫(kù)特點(diǎn)

圖片圖片

什么是 .csv 文件?

.csv 文件是一種用于存儲(chǔ)表格數(shù)據(jù)的文本文件,CSV 是 "Comma-Separated Values" 的縮寫(xiě),意思是 "逗號(hào)分隔值"。CSV 文件是一個(gè)存儲(chǔ)表格和電子表格信息的純文本文件,其內(nèi)容通常是一個(gè)文本、數(shù)字或日期的表格。CSV 文件可以使用以表格形式存儲(chǔ)數(shù)據(jù)的程序輕松導(dǎo)入和導(dǎo)出。

創(chuàng)建控制臺(tái)應(yīng)用

創(chuàng)建一個(gè)名為:CsvHelperExercise的.NET 8控制臺(tái)應(yīng)用。

圖片圖片

圖片圖片

圖片圖片

安裝CsvHelper類庫(kù)

NuGet包管理器中搜索:CsvHelper,點(diǎn)擊安裝!

圖片圖片

定義CSV文件讀取和寫(xiě)入的對(duì)象

public class StudentInfo
    {
        /// <summary>
        /// 學(xué)生學(xué)號(hào)
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// 學(xué)生姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 學(xué)生年齡
        /// </summary>
        public int Age { get; set; }

        /// <summary>
        /// 班級(jí)
        /// </summary>
        public string Class { get; set; }

        /// <summary>
        /// 性別
        /// </summary>
        public string Gender { get; set; }

        /// <summary>
        /// 住址
        /// </summary>
        public string Address { get; set; }
    }

寫(xiě)入CSV文件數(shù)據(jù)

static void Main(string[] args)
        {
            var students = new List<StudentInfo>
            {
                new StudentInfo { ID = 1, Name = "張三", Age = 20, Class = "終極一班", Gender = "男", Address = "北京市東城區(qū)" },
                new StudentInfo { ID = 2, Name = "李四", Age = 21, Class = "終極一班", Gender = "女", Address = "上海市黃浦區(qū)" },
                new StudentInfo { ID = 3, Name = "王五", Age = 22, Class = "終極一班", Gender = "男", Address = "廣州市越秀區(qū)" },
                new StudentInfo { ID = 4, Name = "趙六", Age = 20, Class = "終極二班", Gender = "女", Address = "深圳市福田區(qū)" },
                new StudentInfo { ID = 5, Name = "孫七", Age = 23, Class = "終極二班", Gender = "男", Address = "杭州市西湖區(qū)" },
                new StudentInfo { ID = 6, Name = "周八", Age = 24, Class = "終極二班", Gender = "女", Address = "南京市玄武區(qū)" },
                new StudentInfo { ID = 7, Name = "吳九", Age = 22, Class = "終極二班", Gender = "男", Address = "成都市錦江區(qū)" },
                new StudentInfo { ID = 8, Name = "小袁", Age = 21, Class = "終極三班", Gender = "女", Address = "重慶市渝中區(qū)" },
                new StudentInfo { ID = 9, Name = "大姚", Age = 20, Class = "終極三班", Gender = "男", Address = "武漢市武昌區(qū)" },
                new StudentInfo { ID = 10, Name = "追逐時(shí)光者", Age = 23, Class = "終極三班", Gender = "女", Address = "長(zhǎng)沙市天心區(qū)" }
            };

            //寫(xiě)入CSV文件數(shù)據(jù)
            using var writer = new StreamWriter(@".\StudentInfoFile.csv");
            using var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture);
            csvWriter.WriteRecords(students);
        }

圖片圖片

讀取CSV文件數(shù)據(jù)

快速讀取上面寫(xiě)入到StudentInfoFile.csv中的數(shù)據(jù)。

static void Main(string[] args)
        {
            //讀取CSV文件數(shù)據(jù)
            using var reader = new StreamReader(@".\StudentInfoFile.csv");
            using var csvReader = new CsvReader(reader, CultureInfo.InvariantCulture);
            var getStudentInfos = csvReader.GetRecords<StudentInfo>().ToList();
        }

圖片圖片

項(xiàng)目源碼地址

更多項(xiàng)目實(shí)用功能和特性歡迎前往項(xiàng)目開(kāi)源地址查看??,別忘了給項(xiàng)目一個(gè)Star支持??。

  • CsvHelper類庫(kù)開(kāi)源地址:https://github.com/JoshClose/CsvHelper
  • 文章示例源碼地址:https://github.com/YSGStudyHards/DotNetExercises/tree/master/CsvHelperExercise

優(yōu)秀項(xiàng)目和框架精選

該項(xiàng)目已收錄到C#/.NET/.NET Core優(yōu)秀項(xiàng)目和框架精選中,關(guān)注優(yōu)秀項(xiàng)目和框架精選能讓你及時(shí)了解C#、.NET和.NET Core領(lǐng)域的最新動(dòng)態(tài)和最佳實(shí)踐,提高開(kāi)發(fā)工作效率和質(zhì)量??右淹冢瑲g迎大家踴躍提交PR推薦或自薦(讓優(yōu)秀的項(xiàng)目和框架不被埋沒(méi)??)。

https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.md

參考文章

  • https://www.freecodecamp.org/chinese/news/what-is-a-csv-file-and-how-to-open-the-csv-file-format/
責(zé)任編輯:武曉燕 來(lái)源: 追逐時(shí)光者
相關(guān)推薦

2021-08-02 15:02:37

Go Excelize 開(kāi)發(fā)

2023-01-15 17:11:44

Rust

2023-10-31 12:59:00

C++編程語(yǔ)言

2009-07-10 10:37:11

WINAPI

2019-06-04 15:27:49

InnoDB存儲(chǔ)引擎

2010-02-01 13:26:58

C++讀取CSV文件

2009-11-03 14:22:10

ADO.NET Exc

2024-06-05 09:55:05

.NET數(shù)據(jù)交互

2009-10-30 16:40:04

VB.NET Inte

2011-08-03 17:38:30

iPhone NSUserDefa 自定義

2009-10-27 11:03:16

VB.NET文件夾操作

2023-10-17 16:24:27

PythonCSV

2024-09-23 21:05:45

2010-01-11 11:37:08

VB.NET操作CSV

2009-11-02 12:35:10

VB.NET追加文件

2024-12-03 00:40:55

2023-11-03 11:56:34

2009-11-02 13:54:27

VB.NET shel

2009-11-12 10:06:01

ADO.NET讀取數(shù)據(jù)

2011-04-15 14:37:45

JavaCsv
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)