using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.PrepareHeaderForMatch = (string header,int index) => header.ToLower(); var records = csv.GetRecords<Foo>(); }
using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.HasHeadRecord = false; varrecord = csv.GetRecords<Foo>(); }
using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap<FooMap>(); var records = csv.GetRecords<Foo>(); }
using (var reader = new StremaReader("path\\to\\file.csv"),Encoding.UTF8) { } using (var writer = new StreamWriter("path\\to\\file.csv",Encoding.UTF8)) { }
using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { }
using (var writer = new StreamWriter("path\\to\\file.csv")) using (var csv = new CsvWriter(writer)) { }
读 CSV 文件
获取类记录
将 CSV 插入到对应类的对象中。
数据
1 2
Id,Name 1,one
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { var records = csv.GetRecords<Foo>(); } }
publicclassFoo { publicint Id { get; set; } publicstring Name { get; set; } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { var records = csv.GetRecords<dynamic>(); } }
获取匿名类型的记录
如果你需要将 CSV 插入到匿名类型的对象中,仅仅只要提供匿名类型的定义即可。
数据
1 2
Id,Name 1,one
例子
1 2 3 4 5 6 7 8 9 10 11 12 13
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { var anonymousTypeDefinition = new { Id = default(int), Name = string.Empty }; var records = csv.GetRecords(anonymousTypeDefinition); } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { varrecord = new Foo(); var records = csv.EnumerateRecords(record); foreach (var r in records) { // r is the same instance as record. } } }
publicclassFoo { publicint Id { get; set; } publicstring Name { get; set; } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { var records = new List<Foo>(); csv.Read(); csv.ReadHeader(); while (csv.Read()) { varrecord = new Foo { Id = csv.GetField<int>("Id"), Name = csv.GetField("Name") }; records.Add(record); } } }
publicclassFoo { publicint Id { get; set; } publicstring Name { get; set; } }
voidMain() { 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; }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap<FooMap>(); var records = csv.GetRecords<Foo>(); } }
publicclassFoo { publicint Id { get; set; } publicstring Name { get; set; } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap<FooMap>(); var records = csv.GetRecords<Foo>(); } }
publicclassFoo { publicint Id { get; set; } publicstring Name { getset; } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap<FooMap>(); var records = csv.GetRecords<Foo>(); } }
publicclassFoo { publicint Id { get; set; } publicstring Name { getset; } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap<FooMap>(); var records = csv.GetRecords<Foo>(); } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.HasHeaderRecord = false; csv.Configuration.RegisterClassMap<FooMap>(); var records = csv.GetRecords<Foo>(); } }
publicclassFoo { publicint Id { get; set; } publicstring Name { getset; } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap<FooMap>(); var records = csv.GetRecords<Foo>(); } }
publicclassFoo { publicint Id { get; set; } publicstring Name { get; set; } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap<FooMap>(); var records = csv.GetRecords<Foo>(); } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap<FooMap>(); var records = csv.GetRecords<Foo>(); } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap<FooMap>(); csv.GetRecords<Foo>().ToList().Dump(); } }
publicclassFoo { publicint Id { get; set; } publicstring Name { get; set; } public Json Json { get; set; } }
void Main() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap<FooMap>(); csv.GetRecords<Foo>().ToList().Dump(); } }
public class Foo { public int Id { get; set; } public string Name { get; set; } public Json Json { get; set; } }
public class Json { public string Foo { get; set; } }
public class FooMap : ClassMap<Foo> { public FooMap() { Map(m => m.Id); Map(m => m.Name); Map(m => m.Json).ConvertUsing(row => JsonConvert.DeserializeObject<Json>(row.GetField("Json"))); } }
voidMain() { var records = new List<Foo> { new Foo { Id = 1, Name = "one" } };
using (var writer = new StreamWriter("path\\to\\file.csv")) using (var csv = new CsvWriter(writer)) { csv.Configuration.RegisterClassMap<FooMap>(); csv.WriteRecords(records);
writer.ToString().Dump(); } }
publicclassFoo { publicint Id { get; set; } publicstring Name { get; set; } public Json Json { get; set; } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap<FooMap>(); csv.GetRecords<Foo>().ToList().Dump(); } }
publicclassFoo { publicint Id { get; set; } publicstring Name { get; set; } public DateTimeOffset? Date { get; set; } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.Configuration.RegisterClassMap<FooMap>(); csv.GetRecords<Foo>().ToList().Dump(); } }
publicclassFoo { publicint Id { get; set; } publicstring Name { get; set; } public DateTimeOffset? Date { get; set; } }
voidMain() { using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { csv.GetRecords<Foo>().ToList().Dump(); } }
publicclassFoo { [Name("Identifier")] publicint Id { get; set; }
using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { // Do any configuration to `CsvReader` before creating CsvDataReader. using (var dr = new CsvDataReader(csv)) { var dt = new DataTable(); dt.Load(dr); } }
如果你想要指定行和行的类型,数据表也可以进行自动的类型转换。
1 2 3 4 5 6 7 8 9 10 11 12 13
using (var reader = new StreamReader("path\\to\\file.csv")) using (var csv = new CsvReader(reader)) { // 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));