A fluent, strongly-typed CSV document model for OfficeIMO.
$ dotnet add package OfficeIMO.CSVFluent, strongly typed, AOT-friendly CSV document model aligned with the OfficeIMO ecosystem (Word, Excel, etc.). Targets netstandard2.0, net8.0, net9.0, and net472, with streaming support for large files.
Get<T>(), helpers (AsString/AsInt32), explicit mapping builder for POCOs/records.dynamic, no codegen; trimming-safe delegates only.List<string[]>).using OfficeIMO.CSV;
var csv = new CsvDocument()
.WithDelimiter(';')
.WithHeader("Name", "Age", "City")
.AddRow("Przemek", 36, "Mikołów")
.AddRow("Dominika", 30, "Mikołów")
.SortBy("Age")
.Filter(r => r.AsString("City") == "Mikołów")
.Save("people.csv");
Load & parse from text or file:
var doc = CsvDocument.Load("input.csv", new CsvLoadOptions { Delimiter = ';' });
// or
var doc2 = CsvDocument.Parse(csvText);
AddRow, AddColumn(name, row => ...), RemoveColumn(name)SortBy("Age"), SortBy<TKey>(r => r.Get<int>("Age"), descending: true)Filter(r => r.AsString("City") == "Mikołów")Transform(doc => ...) for advanced scenariosvar validated = CsvDocument.Load("input.csv")
.EnsureSchema(schema => schema
.Column("Id").AsInt32().Required()
.Column("Name").AsString().Required()
.Column("Age").AsInt32().Optional()
)
.ValidateOrThrow();
Retrieve errors without throwing:
validated.Validate(out var errors);
public sealed record Person(int Id, string Name, int Age, string City);
var people = CsvDocument.Load("people.csv")
.Map<Person>(map => map
.FromColumn<int>("Id", (p, v) => p with { Id = v })
.FromColumn<string>("Name", (p, v) => p with { Name = v })
.FromColumn<int>("Age", (p, v) => p with { Age = v })
.FromColumn<string>("City", (p, v) => p with { City = v })
)
.ToList();
foreach (var row in CsvDocument.Load("large.csv", new CsvLoadOptions
{
Mode = CsvLoadMode.Stream,
HasHeaderRow = true
}).AsEnumerable())
{
var id = row.AsInt32("Id");
// process lazily
}
// Need transforms? Materialize explicitly
var materialized = doc.Materialize().SortBy("Id");
CsvLoadOptions: Delimiter, HasHeaderRow (default true), TrimWhitespace (default true), AllowEmptyLines, Culture, Encoding, Mode (InMemory/Stream).CsvSaveOptions: Delimiter, IncludeHeader (default true), NewLine, Culture, Encoding.dotnet add package OfficeIMO.CSV
Not seeing it on NuGet yet? During early development you can add a
ProjectReferencetoOfficeIMO.CSV.csprojin this repo.
netstandard2.0, net8.0, net9.0, net472.