Skip to content
Overview

Overview

A .NET library that imports CSV, TSV, Excel (.xlsx and .xls) and pasted spreadsheet data into your application, with column mapping, value mapping and row validation, for developers who build onboarding and bulk-edit screens.

On this page

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 every dotnet add package command needs --prerelease.
  • Klyroo.Spreadsheet.Core alone (the importer and the .xlsx reader) runs in any .NET 11 app, with no other dependency.
  • A Blazor app for Klyroo.Spreadsheet: 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 the .Client project.
  • Klyroo.UI set up in the app. Klyroo.Spreadsheet brings the Klyroo.UI package, and AddKlyrooSpreadsheet() registers its services, but the layout still needs the Klyroo.UI stylesheets in <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 under obj/klyroo. Node.js is not needed.

To build this repository you need the .NET 11 SDK (a newer SDK works too, see global.json) and a clone of klyroo-ui in the same parent folder: some projects reference it by relative path (../klyroo-ui/...). See Contributing.

Installation

Shell
dotnet add package Klyroo.Spreadsheet.Core --prerelease   # engine only
dotnet add package Klyroo.Spreadsheet --prerelease        # engine and Blazor wizard

For the Blazor wizard, register the services in Program.cs (or MauiProgram.cs), import the namespace in _Imports.razor, and load the stylesheets after the ones of Klyroo.UI:

C#
using Klyroo.Spreadsheet;

builder.Services.AddKlyrooSpreadsheet();
Razor
@using Klyroo.Spreadsheet
HTML
<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.Spreadsheet.Core:

C#
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<PriceRow>, which carries the parsed row in Value.

In Blazor, give the same importer to KlySpreadsheetImport. Users choose or drop a CSV, TSV or .xlsx file on its first step, or paste rows copied from a spreadsheet:

Razor
@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);
    }
}

KlySpreadsheetImport takes the non-generic SpreadsheetImporter, because it reads and edits cells as text and never touches your row type, so a SpreadsheetImporter<PriceRow> 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.Spreadsheet.Core targets net11.0, has no package dependencies and is marked IsTrimmable and IsAotCompatible.
  • Klyroo.Spreadsheet 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.
  • Klyroo.Spreadsheet declares the browser, ios, android, maccatalyst and windows platforms for .NET MAUI Blazor Hybrid.
  • Delimited text (CSV, TSV, semicolon-separated), .xlsx workbooks and legacy .xls workbooks (Excel 97 to 2003) are read. Formulas are not evaluated, and password-protected or Excel 5.0/95 .xls files are refused.

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.

License

MIT