Fluent API for importing CSV and Excel (XLSX) data into .NET 8+ with customizable property mapping, validation, type conversion, and support for init-only properties. Handles headers normalization, regex validation, custom converters, and detailed error reporting.
$ dotnet add package PandaTech.FluentImporterFluent API for importing CSV and Excel data into .NET 8+ applications with customizable property mapping and validation.
dotnet add package PandaTech.FluentImporter
public class FileData
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime Date { get; set; }
public string? Comment { get; set; }
public DateTime CreatedAt { get; init; }
public string CreatedBy { get; init; }
}
public class FileDataImportRule : ImportRule<FileData>
{
public FileDataImportRule()
{
RuleFor(x => x.Name)
.NotEmpty();
RuleFor(x => x.Description)
.ReadFromColumn("Description text")
.Default("No Description");
RuleFor(x => x.Date)
.ReadFromColumn("Date")
.Convert(DateTime.Parse);
RuleFor(x => x.Comment)
.ReadFromColumn("Comment");
RuleFor(x => x.Id)
.ReadFromColumn("Id")
.Convert(s => long.Parse(s));
RuleFor(x => x.CreatedAt)
.WriteValue(DateTime.UtcNow);
RuleFor(x => x.CreatedBy)
.ReadFromModel(x => x.CreatedBy + " - Modified");
}
}
var importRule = new FileDataImportRule();
// From CSV file
var data = importRule.ReadCsv("path/to/file.csv");
// From CSV stream
using var stream = File.OpenRead("file.csv");
var data = importRule.ReadCsv(stream);
// From Excel file
var data = importRule.ReadXlsx("path/to/file.xlsx");
// From Excel stream
using var stream = File.OpenRead("file.xlsx");
var data = importRule.ReadXlsx(stream);
// From in-memory data
var dict = new List<Dictionary<string, string>>
{
new() { ["Name"] = "John", ["Date"] = "2024-01-01" }
};
var data = importRule.GetRecords(dict);
| Method | Description |
|---|---|
ReadFromColumn(string) | Map to a different column name |
NotEmpty() | Require non-empty value |
Default(T) | Set default value if null/empty |
Convert(Func<string, T>) | Custom converter function |
Convert(Func<string, TModel, T>) | Converter with access to model instance |
Validate(string) | Regex validation pattern |
WriteValue(T) | Set a constant value |
ReadFromModel(Func<TModel, T>) | Compute value from model |
string, int, long, decimal, double, bool, etc.DateTime, DateTimeOffset, GuidNullable<T> for all value typesEnum types (case-insensitive)Convert() methodThe library automatically handles multiple boolean representations:
true/false1/0yes/noAll import exceptions inherit from ImportException:
EmptyFileImportException - File contains no data rowsInvalidCellValueException - Cell value failed validation or conversionInvalidColumnValueException - Required column not foundInvalidPropertyNameException - Property doesn't exist on modelEach exception includes:
MIT