CsvHelper

来自软件开发
跳转至: 导航搜索

CsvHelper

源码地址:https://github.com/JoshClose/CsvHelper

NuGet安装:

Install-Package CsvHelper

.Net 4.0支持的最高版本是v2.16.3.0

Install-Package CsvHelper -Version 2.16.3.0

简单用法:

读取数据:

void Main()
{
    using (var reader = new StreamReader("path\\to\\file.csv"))
    using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
    {
        var records = csv.GetRecords<Foo>();
    }
}

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

动态类型:

void Main()
{
    using (var reader = new StreamReader("path\\to\\file.csv"))
    using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
    {
        var records = csv.GetRecords<dynamic>();
    }
}

Reading by Hand

void Main()
{
    using (var reader = new StreamReader("path\\to\\file.csv"))
    using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
    {
        var records = new List<Foo>();
        csv.Read();
        csv.ReadHeader();
        while (csv.Read())
        {
            var record = new Foo
            {
                Id = csv.GetField<int>("Id"),
                Name = csv.GetField("Name")
            };
            records.Add(record);
        }
    }
}

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

DataTable

using (var reader = new StreamReader("path\\to\\file.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
    // Do any configuration to `CsvReader` before creating CsvDataReader.
    using (var dr = new CsvDataReader(csv))
    {        
        var dt = new DataTable();
        dt.Load(dr);
    }
}
using (var reader = new StreamReader("path\\to\\file.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
    // Do any configuration to `CsvReader` before creating CsvDataReader.
    using (var dr = new CsvDataReader(csv))
    {        
        var dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));

        dt.Load(dr);
    }
}
using (var reader = new StreamReader("data.csv", Encoding.UTF8))
using (var csv = new CsvReader(reader, CultureInfo.CurrentCulture))
{
    ProvincialItems = csv.GetRecords<ProvincialInfo>().ToList();
}

示例代码:

void Main()
{
    using (var reader = new StreamReader("path\\to\\file.csv"))
    using (var csv = new CsvReader(reader))
    {
        csv.Configuration.IgnoreBlankLines = false;
        csv.Configuration.RegisterClassMap<FooMap>();
        csv.Configuration.RegisterClassMap<BarMap>();
        var fooRecords = new List<Foo>();
        var barRecords = new List<Bar>();
        var isHeader = true;
        while (csv.Read())
        {
            if (isHeader)
            {
                csv.ReadHeader();
                isHeader = false;
                continue;
            }

            if (string.IsNullOrEmpty(csv.GetField(0)))
            {
                isHeader = true;
                continue;
            }

            switch (csv.Context.HeaderRecord[0])
            {
                case "FooId":
                    fooRecords.Add(csv.GetRecord<Foo>());
                    break;
                case "BarId":
                    barRecords.Add(csv.GetRecord<Bar>());
                    break;
                default:
                    throw new InvalidOperationException("Unknown record type.");
            }
        }
    }
}

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

public class Bar
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

public sealed class FooMap : ClassMap<Foo>
{
    public FooMap()
    {
        Map(m => m.Id).Name("FooId");
        Map(m => m.Name);
    }
}

public sealed class BarMap : ClassMap<Bar>
{
    public BarMap()
    {
        Map(m => m.Id).Name("BarId");
        Map(m => m.Name);
    }
}

Really simple csv library

Tiny CSV Parser

官网:http://bytefish.github.io/TinyCsvParser/index.html

安装:

Install-Package TinyCsvParser

参考资料