PandaTech.FluentImporter
3.0.0
dotnet add package PandaTech.FluentImporter --version 3.0.0
NuGet\Install-Package PandaTech.FluentImporter -Version 3.0.0
<PackageReference Include="PandaTech.FluentImporter" Version="3.0.0" />
paket add PandaTech.FluentImporter --version 3.0.0
#r "nuget: PandaTech.FluentImporter, 3.0.0"
// Install PandaTech.FluentImporter as a Cake Addin #addin nuget:?package=PandaTech.FluentImporter&version=3.0.0 // Install PandaTech.FluentImporter as a Cake Tool #tool nuget:?package=PandaTech.FluentImporter&version=3.0.0
Pandatech FluentImporter
The Pandatech.FluentImporter
is a lightweight NuGet package designed to facilitate the importation of CSV and Excel
data
into .NET 8 or higher applications. Featuring a fluent API, the FluentImporter enables developers to specify custom
import
rules for each model property, making the data import process both straightforward and flexible.
Features
- Effortlessly import CSV and XLSX (excel) data.
- Define custom import rules using a fluent API.
Installation
You can install Pandatech FileImporter via NuGet Package Manager or .NET CLI:
dotnet add package PandaTech.FluentImporter
Usage
Define Model
First, define a model class with properties that represent the data fields you wish to import. For example:
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; set; }
public string CreatedBy { get; set; }
}
Define Import Rules
Next, create a class for import rules by inheriting from ImportRule<TModel>
and define your rules using the fluent
API. For example:
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 => BaseConverter.PandaBaseConverter.Base36ToBase10(s)!.Value);
RuleFor(x => x.CreatedAt).WriteValue(DateTime.UtcNow);
RuleFor(x => x.CreatedBy).WriteValue("System");
RuleFor(x => x.CreatedBy).ReadFromModel(x => x.CreatedBy + " - 1");
}
}
Import Data
Finally, use the methods provided in your import rule instance to import data from CSV, Excel, or in-memory sources.
Import from In-Memory Data
IEnumerable<TModel> GetRecords(IEnumerable<Dictionary<string, string>> data)
Import from CSV
List<TModel> ReadCsv(Stream csvStream)
List<TModel> ReadCsv(string csvFilePath)
Import from Excel
List<TModel> ReadXlsx(Stream stream)
List<TModel> ReadXlsx(string csvFilePath)
Example
var importRule = new FileDataImportRule();
var csvFilePath = "path/to/your/file.csv";
var importedData = importRule.ReadCsv(csvFilePath);
Contributing
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. |
NuGet packages (2)
Showing the top 2 NuGet packages that depend on PandaTech.FluentImporter:
Package | Downloads |
---|---|
Pandatech.ResponseCrafter
Handling exceptions, custom Dtos. |
|
Pandatech.SharedKernel.Postgres
Pandatech.SharedKernel.Postgres simplifies PostgreSQL integration in ASP.NET Core applications by providing utilities for Entity Framework Core setup, health checks, and other enhancements. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.0.0 | 57 | 11/21/2024 |
2.0.13 | 160 | 9/4/2024 |
2.0.12 | 393 | 5/16/2024 |
2.0.11 | 100 | 5/16/2024 |
2.0.10 | 87 | 5/16/2024 |
2.0.9 | 111 | 5/15/2024 |
2.0.8 | 92 | 5/13/2024 |
2.0.7 | 101 | 5/8/2024 |
2.0.6 | 162 | 5/6/2024 |
2.0.5 | 110 | 5/6/2024 |
2.0.4 | 119 | 5/5/2024 |
2.0.3 | 95 | 5/3/2024 |
2.0.2 | 84 | 5/3/2024 |
2.0.1 | 122 | 4/18/2024 |
2.0.0 | 104 | 4/17/2024 |
Upgrade to .net 9