Getting started
In this tutorial you import a small price list: first from a console app with Klyroo., then through the Blazor wizard. It takes about ten minutes.
On this page
Check the prerequisites first: the .NET 11 SDK, and for the components a Blazor app with Klyroo.UI set up.
1. Install
Pick the package for what you build. The Blazor package includes the Core package, so you never install both.
| You build | Package |
|---|---|
| A console app, API, worker, or your own UI | Klyroo. |
| A Blazor Web App or a .NET MAUI Blazor Hybrid app | Klyroo. |
dotnet add package Klyroo.Spreadsheet.Core --prerelease2. Describe the data you expect
A schema lists the columns your application wants, and it is typed by the class you want one row to become. Write that class first, then pick its properties with Spreadsheet. Each Column takes a member expression, so the property name is the column key, and the kind of cell follows the property type: text for string, a number for int, decimal and the other numeric types, yes or no for bool, a date for DateOnly, DateTime and DateTimeOffset, and a choice for an enum. The expression is read, never compiled, so it works with NativeAOT.
This schema expects a SKU, a price written with a decimal comma, and a currency from a fixed list:
using Klyroo.Spreadsheet;
public enum Currency { Eur, Usd }
public sealed class PriceRow
{
public string? Sku { get; set; }
public decimal Price { get; set; }
public Currency Currency { get; set; }
}
var schema = SpreadsheetSchema.For<PriceRow>(s =>
{
s.Column(r => r.Sku, c => c.Label("SKU").Required());
s.Column(r => r.Price, c => c.Label("Unit price").DecimalSeparator(',').Rule(SpreadsheetRule.Min(0m)));
s.Column(r => r.Currency, c => c.Options(
[
new SpreadsheetOption<Currency>(Currency.Eur, "Euro", ["€"]),
new SpreadsheetOption<Currency>(Currency.Usd, "US dollar", ["$"]),
]));
});The row class needs a public parameterless constructor and public setters; for a type without one, pass a factory: Spreadsheet. A property that cannot hold an empty cell (decimal, bool, an enum) makes its column required, so Price and Currency above are required without saying so; use decimal? for an optional price. An enum column offers one option per distinct value without Options (a member that repeats another's value, such as Medium = Normal, is accepted as an alias of the first declared one); Options replaces them to add labels and aliases such as the € symbol.
A column without a Label gets one from its key (UnitPrice becomes Unit price). File headers are matched against the key, the label and the optional Headers aliases.
3. Load a file
Create a Spreadsheet for the schema and give it the text of a file. LoadText detects the separator (tab, semicolon or comma), the header row and the columns:
var importer = new SpreadsheetImporter<PriceRow>(schema);
importer.LoadText("SKU;Unit price;Currency\nA-1;12,50;€\nA-2;-3;USD", "prices.csv");
Console.WriteLine(importer.FileLoaded); // True
Console.WriteLine(importer.ImportableRows.Count); // 1
Console.WriteLine(importer.InvalidRows.Count); // 1The second row has a negative price, so the Min rule gives it an error and it is not importable. Inspect why:
foreach (var row in importer.InvalidRows)
{
foreach (var issue in row.Issues)
{
Console.WriteLine($"row {row.RowNumber}: {issue.Field}: {issue.Message}");
}
}
// row 3: Price: Value is below 0The importer never changes your file. It holds what the user decided (column choices, cell edits, discarded rows) and recomputes the rows after each change.
4. Send the rows to your code
OnSubmit receives the importable rows as SpreadsheetRow, which carries the parsed row in Value, and returns the rows your server refused, or null when it accepted all of them. Set it on the builder of step 2:
var schema = SpreadsheetSchema.For<PriceRow>(s =>
{
// columns as above
s.OnSubmit = rows =>
{
foreach (var row in rows)
{
Console.WriteLine($"{row.Value.Sku}: {row.Value.Price} {row.Value.Currency}");
}
return Task.FromResult<IReadOnlyList<SpreadsheetRejectedRow>?>(null);
};
});Then submit:
await importer.SubmitAsync(); // OnSubmit prints: A-1: 12.5 Eur
Console.WriteLine(importer.ImportedCount); // 1row. is a PriceRow with the price as a decimal and the currency as a Currency. A cell that is empty without a default, or that could not be read, leaves its property at the default of its type, but the rows OnSubmit receives are importable, so every required value is there. The complete program of steps 2 to 4 is the sample in the README.
5. Add the Blazor wizard
Install the UI package instead of the Core one in your Blazor project:
dotnet add package Klyroo.Spreadsheet --prereleaseRegister the services. In a Blazor Web App that uses WebAssembly or Auto, call it in both the server and the client project:
using Klyroo.Spreadsheet;
builder.Services.AddKlyrooSpreadsheet();Import the namespace in _:
@using Klyroo.SpreadsheetLoad the stylesheets in App. or wwwroot/, after the two stylesheets of Klyroo.UI:
<link rel="stylesheet" href="_content/Klyroo.UI/klyroo.preflight.css" />
<link rel="stylesheet" href="_content/Klyroo.UI/klyroo.css" />
<link rel="stylesheet" href="_content/Klyroo.Spreadsheet/klyroo-spreadsheet.css" />6. Build the page
KlySpreadsheet renders the five steps of the wizard (file, columns, values, review, import) for an importer you give it. The component takes the non-generic Spreadsheet, because it reads and edits cells as text and never touches your row class, so a Spreadsheet is passed as it is. Its first step lets users choose or drop a file (CSV, TSV, text, . or ., 10 MB at most by default, see MaxFileSize) or paste rows:
@page "/prices/import"
@using Klyroo.Spreadsheet
<KlySpreadsheetImport Importer="_importer" Title="Import prices" />
@code {
private SpreadsheetImporter<PriceRow> _importer = default!;
protected override void OnInitialized()
{
_importer = new SpreadsheetImporter<PriceRow>(SpreadsheetSchema.For<PriceRow>(s =>
{
s.Column(r => r.Sku, c => c.Label("SKU").Required());
s.Column(r => r.Price);
s.OnSubmit = SaveAsync;
}));
}
private Task<IReadOnlyList<SpreadsheetRejectedRow>?> SaveAsync(IReadOnlyList<SpreadsheetRow<PriceRow>> rows)
{
// Persist rows.Select(r => r.Value) here. Return the rows the server refused, or null.
return Task.FromResult<IReadOnlyList<SpreadsheetRejectedRow>?>(null);
}
}The page needs an interactive render mode, set on the page or for the whole app. After the file loads, the wizard shows a Continue button. It skips the columns step when every required column matched, and the values step when no select value is unknown, so a clean file goes straight to the review.
The component subscribes to the importer's Changed event, so a file your own code loads with LoadText, LoadFileAsync or LoadWorkbook shows up too. Keep the importer in a field of the page (one per user and per import), not in a singleton service.