C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# csvhelper实现csv操作

C#使用csvhelper实现csv的基本操作

作者:假装我不帅

CsvHelper 是一个用于读写 CSV 文件的.NET库,极其快速,灵活且易于使用,CsvHelper 建立在.NET Standard 2.0 之上,几乎可以在任何地方运行,本文给大家介绍了C#使用csvhelper实现csv的基本操作,需要的朋友可以参考下

新建控制台项目

安装csvhelper 33.0.1

在这里插入图片描述

写入csv

新建Foo.cs

namespace CsvSut02;

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}

批量写入

using System.Globalization;
using CsvHelper;
using CsvHelper.Configuration;

namespace CsvSut02;

class Program
{
    static void Main(string[] args)
    {
        List<Foo> foos = new List<Foo>();
        for (int i = 0; i < 100; i++)
        {
            foos.Add(new Foo()
            {
                Id = i,
                Name = "我是" + i
            });
        }

        var fileName = $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}.csv";
        using (var writer = new StreamWriter(fileName))
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture);
            using (var csv = new CsvWriter(writer, config))
            {
                csv.WriteRecords(foos);
            }
        }
    }
}

在这里插入图片描述

逐条写入

using System.Globalization;
using CsvHelper;
using CsvHelper.Configuration;

namespace CsvSut02;

class Program
{
    static void Main(string[] args)
    {
        List<Foo> foos = new List<Foo>();
        for (int i = 0; i < 100; i++)
        {
            foos.Add(new Foo()
            {
                Id = i,
                Name = "我是" + i
            });
        }

        var fileName = $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}.csv";
        using (var writer = new StreamWriter(fileName))
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture);
            using (var csv = new CsvWriter(writer, config))
            {
	            //这里看过来
                foreach (var foo in foos)
                {
                    csv.WriteRecord(foo);
                    csv.NextRecord();
                }
            }
        }
    }
}

在这里插入图片描述

增加标题

特性方式

using CsvHelper.Configuration.Attributes;

namespace CsvSut02;

public class Foo
{
    [Name("Id")]
    public int Id { get; set; }
    
    [Name("Name")]
    public string Name { get; set; }
}

using System.Globalization;
using CsvHelper;
using CsvHelper.Configuration;

namespace CsvSut02;

class Program
{
    static void Main(string[] args)
    {
        List<Foo> foos = new List<Foo>();
        for (int i = 0; i < 100; i++)
        {
            foos.Add(new Foo()
            {
                Id = i,
                Name = "我是" + i
            });
        }

        var fileName = $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}.csv";
        using (var writer = new StreamWriter(fileName))
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture);
            using (var csv = new CsvWriter(writer, config))
            {
                csv.WriteHeader<Foo>();
                csv.NextRecord(); 
                foreach (var foo in foos)
                {
                    csv.WriteRecord(foo);
                    csv.NextRecord();
                }
            }
        }
    }
}

在这里插入图片描述

配置类

新建FooClassMap.cs

using CsvHelper.Configuration;

namespace CsvSut02;

public class FooClassMap : ClassMap<Foo>
{
    public FooClassMap()
    {
        Map(m => m.Id)
            .Index(0)
            .Name("id");
        Map(m => m.Name)
            .Index(1)
            .Name("name");
    }
}

修改Program.cs

using System.Globalization;
using CsvHelper;
using CsvHelper.Configuration;

namespace CsvSut02;

class Program
{
    static void Main(string[] args)
    {
        List<Foo> foos = new List<Foo>();
        for (int i = 0; i < 100; i++)
        {
            foos.Add(new Foo()
            {
                Id = i,
                Name = "我是" + i
            });
        }

        var fileName = $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}.csv";
        using (var writer = new StreamWriter(fileName))
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture);
            using (var csv = new CsvWriter(writer, config))
            {
                csv.Context.RegisterClassMap<FooClassMap>();
                csv.WriteHeader<Foo>();
                csv.NextRecord();
                foreach (var foo in foos)
                {
                    csv.WriteRecord(foo);
                    csv.NextRecord();
                }
            }
        }
    }
}

在这里插入图片描述

可能有人觉得麻烦,使用配置类可以保证原始类的纯粹,保证poco

读取csv

读取和写入一样,可以两种方式实现一个是配置类一个是特性,下面都以特性演示

无标题csv

在这里插入图片描述

using CsvHelper.Configuration.Attributes;

namespace CsvSut02;

public class Foo
{
    [Index(0)]
    public int Id { get; set; }
    
    [Index(1)]
    public string Name { get; set; }
}

using System.Globalization;
using CsvHelper;
using CsvHelper.Configuration;

namespace CsvSut02;

class Program
{
    static void Main(string[] args)
    {
        var file = "2024_07_24_18_24_00.csv";
        using (var reader = new StreamReader(file))
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture);
            config.HasHeaderRecord = false;//没有头部
            using (var csv = new CsvReader(reader, config))
            {
                var records = csv.GetRecords<Foo>().ToList();
                foreach (var record in records)
                {
                    Console.WriteLine(record.Id);   
                }
            }
        }
    }
}

在这里插入图片描述

有标题csv

在这里插入图片描述

using CsvHelper.Configuration.Attributes;

namespace CsvSut02;

public class Foo
{
    [Name("id")]
    public int Id { get; set; }
    
    [Name("name")]
    public string Name { get; set; }
}

using CsvHelper.Configuration.Attributes;

namespace CsvSut02;

public class Foo
{
    [Name("id")]
    public int Id { get; set; }
    
    [Name("name")]
    public string Name { get; set; }
}

using System.Globalization;
using CsvHelper;
using CsvHelper.Configuration;

namespace CsvSut02;

class Program
{
    static void Main(string[] args)
    {
        var file = "2024_07_24_18_30_49.csv";
        using (var reader = new StreamReader(file))
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture);
            config.HasHeaderRecord = true;//有头部
            using (var csv = new CsvReader(reader, config))
            {
                var records = csv.GetRecords<Foo>().ToList();
                foreach (var record in records)
                {
                    Console.WriteLine(record.Id);   
                }
            }
        }
    }
}

在这里插入图片描述

增量写入

using System.Globalization;
using System.Text;
using CsvHelper;
using CsvHelper.Configuration;

namespace CsvSut02;

class Program
{
    static void Main(string[] args)
    {
        var fileName = $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}.csv";
        WriteModelWithAppend(fileName);
        WriteModelWithAppend(fileName);
    }
    
    /// <summary>
    /// 追加方式写入
    /// </summary>
    /// <param name="fileName"></param>
    static void WriteModelWithAppend(string fileName)
    {
        var model = new Foo();
        model.Id = 1;
        model.Name = "测试";
        // 检查文件是否存在,以确定是否应该以追加模式打开  
        var mode = File.Exists(fileName) ? FileMode.Append : FileMode.Create;
        var config = new CsvConfiguration(CultureInfo.InvariantCulture);
        using var writer = new StreamWriter(new FileStream(fileName, mode, FileAccess.Write, FileShare.Read),
            Encoding.UTF8);
        using var csv = new CsvWriter(writer, config);
        
        if (mode == FileMode.Create)
        {
            csv.WriteHeader<Foo>();
            csv.NextRecord(); 
        }
        csv.WriteRecord(model);
        csv.NextRecord(); 
    }
}

在这里插入图片描述

注意多线程有问题需要加锁

以上就是C#使用csvhelper实现csv的基本操作的详细内容,更多关于C# csvhelper实现csv操作的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文