Overview
A .NET library that imports CSV, TSV, Excel (. and .) and pasted spreadsheet data into your application, with column mapping, value mapping and row validation, for developers who build onboarding and bulk-edit screens.
User files rarely match your model: the header is on row 3, a column is named differently, a status says "Gold" where you expect "pro". Klyroo.Spreadsheet puts the engine that deals with this in a headless package you can unit test, and adds a Blazor wizard on top of it. The engine runs anywhere .NET 11 runs, and the wizard runs in Blazor Web Apps and .NET MAUI Blazor Hybrid.
Prerequisites
To use the library in an app:
- The .NET 11 SDK, and an app that targets
net11.0. The packages are prereleases, so everydotnet add packagecommand needs--prerelease. Klyroo.alone (the importer and theSpreadsheet. Core .reader) runs in any .NET 11 app, with no other dependency.xlsx - A Blazor app for
Klyroo.: a Blazor Web App or a .NET MAUI Blazor Hybrid app. The wizard, including the file picker, needs an interactive render mode (Interactive Server, WebAssembly or Auto). In a WebAssembly or Auto app, register the services in the server project and in theSpreadsheet .project.Client - Klyroo.UI set up in the app.
Klyroo.brings the Klyroo.UI package, andSpreadsheet AddKlyrooregisters its services, but the layout still needs the Klyroo.UI stylesheets inSpreadsheet() <head>and<KlyApp>around the body, as described in the Klyroo.UI installation. - A browser with the native popover API, which Klyroo.UI uses: Chrome and Edge 114, Safari 17 or Firefox 125, or later. In a .NET MAUI app, that means iOS or Mac Catalyst 17, or an Android System WebView 114 or later.
- Network access on the first build only if your app self-hosts web fonts (
KlyrooFont), uses icons outside the built-in set, or compiles Tailwind (KlyrooTailwind): the build downloads them once and caches them underobj/. Node.js is not needed.klyroo
To build this repository you need the .NET 11 SDK (a newer SDK works too, see global.) and a clone of klyroo-ui in the same parent folder: some projects reference it by relative path (../). See Contributing.
Installation
dotnet add package Klyroo.Spreadsheet.Core --prerelease # engine only
dotnet add package Klyroo.Spreadsheet --prerelease # engine and Blazor wizardFor the Blazor wizard, register the services in Program. (or MauiProgram.), import the namespace in _, and load the stylesheets after the ones of Klyroo.UI:
using Klyroo.Spreadsheet;
builder.Services.AddKlyrooSpreadsheet();@using Klyroo.Spreadsheet<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" />Quick start
Headless, with Klyroo.:
using Klyroo.Spreadsheet;
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(',').Required().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", ["$"]),
]));
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);
};
});
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.ImportableRows.Count); // 1
Console.WriteLine(importer.InvalidRows.Count); // 1: A-2 has a negative price
await importer.SubmitAsync();
public enum Currency { Eur, Usd }
public sealed class PriceRow
{
public string? Sku { get; set; }
public decimal Price { get; set; }
public Currency Currency { get; set; }
}The schema is typed by a row class. Each column is a property picked with a member expression, and its kind 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 or for any column with Options. A property that cannot hold an empty cell (decimal, bool, an enum) is required, so use decimal? when the cell may stay empty. The callbacks receive SpreadsheetRow, which carries the parsed row in Value.
In Blazor, give the same importer to KlySpreadsheet. Users choose or drop a CSV, TSV or . file on its first step, or paste rows copied from a spreadsheet:
@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, c => c.Required());
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);
}
}KlySpreadsheet takes the non-generic Spreadsheet, because it reads and edits cells as text and never touches your row type, so a Spreadsheet is passed as it is.
Documentation
| Getting started | Step-by-step first integration |
| Guides | Task-oriented recipes |
| Concepts | How the library works |
| Platforms | Render modes, .NET MAUI, NativeAOT |
| API reference | Every public type and member |
| FAQ | Troubleshooting and limitations |
Compatibility
- .NET 11 SDK.
Klyroo.targetsSpreadsheet. Core net11.0, has no package dependencies and is markedIsTrimmableandIsAotCompatible.Klyroo.needs an interactive render mode (Interactive Server, WebAssembly or Auto): the component handles events through Blazor, so under Static SSR it renders but does not respond.Spreadsheet Klyroo.declares theSpreadsheet browser,ios,android,maccatalystandwindowsplatforms for .NET MAUI Blazor Hybrid.- Delimited text (CSV, TSV, semicolon-separated),
.workbooks and legacyxlsx .workbooks (Excel 97 to 2003) are read. Formulas are not evaluated, and password-protected or Excel 5.0/95xls .files are refused.xls
Contributing
Contributions are welcome. Read CONTRIBUTING.md before opening a pull request, and follow the code of conduct. Report security issues privately as described in SECURITY.md.