![]() |
VOOZH | about |
dotnet add package Mafe.TinyCsv --version 2.2.2
NuGet\Install-Package Mafe.TinyCsv -Version 2.2.2
<PackageReference Include="Mafe.TinyCsv" Version="2.2.2" />
<PackageVersion Include="Mafe.TinyCsv" Version="2.2.2" />Directory.Packages.props
<PackageReference Include="Mafe.TinyCsv" />Project file
paket add Mafe.TinyCsv --version 2.2.2
#r "nuget: Mafe.TinyCsv, 2.2.2"
#:package Mafe.TinyCsv@2.2.2
#addin nuget:?package=Mafe.TinyCsv&version=2.2.2Install as a Cake Addin
#tool nuget:?package=Mafe.TinyCsv&version=2.2.2Install as a Cake Tool
TinyCsv is a .NET library to read and write CSV data in an easy way.
To use it in your project, add the Mafe.TinyCsv NuGet package to your project.
It is available an AspNetCore extension:
Define the model that you want to use, like this:
public class Model
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public DateTime CreatedOn { get; set; }
public string TextBase64 { get; set; }
}
Now, it is necessary to define the TinyCSV options, like this:
var csv = new TinyCsv<Model>(options =>
{
// Options
options.HasHeaderRecord = true;
options.Delimiter = ";";
options.RowsToSkip = 0;
options.SkipRow = (row, idx) => string.IsNullOrWhiteSpace(row) || row.StartsWith("#");
options.TrimData = true;
options.AllowRowEnclosedInDoubleQuotesValues = true;
options.EnableHandlers = false;
options.ValidateColumnCount = true;
// Columns
options.Columns.AddColumn(m => m.Id);
options.Columns.AddColumn(m => m.Name);
options.Columns.AddColumn(m => m.Price);
options.Columns.AddColumn(m => m.CreatedOn, "dd/MM/yyyy");
options.Columns.AddColumn(m => m.TextBase64, new Base64Converter());
});
The options defines that the file has the header in first row and the delimier char is ";", furthermore there are defined some columns. RowsToSkip and SkipRow are used to skip the first rows of the file. TrimData is used to remove the white spaces from the data.
It is possible to define a custom converter for a column, like this:
public class Base64Converter : IValueConverter
{
public string Convert(object value, object parameter, IFormatProvider provider)
=> Base64Encode($"{value}");
public object ConvertBack(string value, Type targetType, object parameter, IFormatProvider provider)
=> Base64Decode(value);
private string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
private string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
}
Base64Converter is used to convert the data column to a base64 string and vice versa for TextBase64 column.
A column is defined with the model's property only. The library get or set the value in automatically.
To read a csv file is only necessary invoke the load method, like this:
var models = csv.LoadFromFile("file.csv");
foreach(var model in models){
// processing
}
or you can to use the asynchronously method, like this:
var models = await csv.LoadFromFileAsync("file.csv");
await foreach(var model in models){
// processing
}
for NET5_0_OR_GREATER or NETSTANDARD2_1_OR_GREATER, like this:
var models = await csv.LoadFromFileAsync("file.csv").ToListAsync();
The load method returns a collection of Model type items and takes as argument the file's path.
To write a csv file is only necessary invoke the Save method, like this:
csv.Save("file_export.csv", models);
or you can to use the asynchronously method, like this:
await csv.SaveAsync("file_export.csv", models);
The save method takes the file's path and a collection of Model type. The method saves the file.
It's possible to define event handlers for the events of the library, like this:
options.Handlers.Read.RowHeader += (s, e) => Console.WriteLine($"Row header: {e.RowHeader}");
options.Handlers.Read.RowReading += (s, e) => Console.WriteLine($"{e.Index}-{e.Row}");
options.Handlers.Read.RowRead += (s, e) => Console.WriteLine($"{e.Index}-{e.Model}");
and the write handlers are:
options.Handlers.Write.RowHeader += (s, e) => Console.WriteLine($"Row header: {e.RowHeader}");
options.Handlers.Write.RowWriting += (s, e) => Console.WriteLine($"{e.Index} - {e.Model}");
options.Handlers.Write.RowWrittin += (s, e) => Console.WriteLine($"{e.Index} - {e.Row}");
The handlers are enabled if the option "EnableHandlers" is set true value, like this:
var csv = new TinyCsv<Model>(options =>
{
// Options
// ...
options.EnableHandlers = true;
//...
// Columns
// ...
});
You can use, in alternative way, the attributes in the model definition, like this:
[Delimiter(";")]
[RowsToSkip(0)]
[SkipRow(typeof(CustomSkipRow))]
[TrimData(true)]
[ValidateColumnCount(true)]
[HasHeaderRecord(true)]
public class AttributeModel
{
[Column]
public int Id { get; set; }
[Column]
public string Name { get; set; }
[Column]
public decimal Price { get; set; }
[Column(format: "dd/MM/yyyy")]
public DateTime CreatedOn { get; set; }
[Column(converter: typeof(Base64Converter))]
public string TextBase64 { get; set; }
public override string ToString()
{
return $"ToString: {Id}, {Name}, {Price}, {CreatedOn}, {TextBase64}";
}
}
class CustomSkipRow : ISkipRow
{
public Func<string, int, bool> SkipRow { get; } = (row, idx) => string.IsNullOrWhiteSpace(row) || row.StartsWith("#");
}
class Base64Converter : IValueConverter {
...
}
and you can use it, like this
var csv = new TinyCsv<AttributeModel>();
Is available the Mafe.TinyCsv.Extensions library to use the TinyCsv in your project and it is downloadable in the NuGet package.
It's possible to use the extensions methods in your project, like this:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddTinyCsv<Model1>("Model1", options =>
{
// Options
options.HasHeaderRecord = true;
options.Delimiter = ";";
options.SkipRow = (row, idx) => string.IsNullOrWhiteSpace(row) || row.StartsWith("#");
// columns
options.Columns.AddColumn(m => m.Id);
options.Columns.AddColumn(m => m.Name);
options.Columns.AddColumn(m => m.Price);
});
var app = builder.Build();
app.MapGet("/csv1", [AllowAnonymous] async (ITinyCsvFactory tinyCsvFactory) =>
{
var tinyCsv = tinyCsvFactory.Get<Model1>("Model1");
var result = await tinyCsv.LoadFromFileAsync("model1.csv");
Console.WriteLine($"{result?.Count()}");
return result;
});
app.MapGet("/csv2", [AllowAnonymous] async (ITinyCsvFactory tinyCsvFactory) =>
{
var tinyCsv = tinyCsvFactory.Create<Model2>(options =>
{
options.HasHeaderRecord = true;
options.Delimiter = ";";
options.SkipRow = (row, idx) => string.IsNullOrWhiteSpace(row) || row.StartsWith("#");
options.Columns.AddColumn(m => m.Id);
options.Columns.AddColumn(m => m.Name);
});
var result = await tinyCsv.LoadFromFileAsync("model2.csv");
Console.WriteLine($"{result?.Count()}");
return result;
});
app.Run();
The AddTinyCsv method extension takes the name of the model (the name is a unique key) and the options. The method defines a TinyCsv instance.
The specific model is retriving with the Get method, like this:
var tinyCsv = tinyCsvFactory.Get<Model1>("Model1");
The library is very very simple to use.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 is compatible. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net452 net452 is compatible. net46 net46 was computed. net461 net461 was computed. net462 net462 is compatible. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 is compatible. net48 net48 is compatible. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 4 NuGet packages that depend on Mafe.TinyCsv:
| Package | Downloads |
|---|---|
|
DarkStar.Api
Package Description |
|
|
Mafe.TinyCsv.Extensions
TinyCsv Extensions is a .NET library utilities to read and write CSV data in an easy way. |
|
|
Mafe.TinyCsv.AspNetCore
TinyCsv AspNetCore is a .NET library utilities to read and write CSV data in an easy way on AspNetCore project. |
|
|
Void.Api
Api library for void engine mmo |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 2.2.2 | 8,017 | 9/9/2024 | |
| 2.2.1 | 3,363 | 5/23/2024 | |
| 2.2.0 | 1,310 | 4/6/2024 | 2.2.0 is deprecated because it has critical bugs. |
| 2.1.0 | 531 | 9/5/2023 | |
| 2.0.0 | 395 | 8/22/2023 | |
| 1.6.1 | 1,856 | 11/22/2022 | |
| 1.6.0 | 930 | 10/19/2022 | 1.6.0 is deprecated. |
| 1.5.2 | 22,424 | 6/23/2022 | |
| 1.5.1 | 948 | 6/22/2022 | 1.5.1 is deprecated because it is no longer maintained. |