TX Sheet Control

ASP.NET Core

Topic

In an ASP.NET Core Razor Pages application, the spreadsheet editor is a Tag Helper. One tx-sheet-control element in a Razor page renders the editor and brings along the scripts it needs, with bundled styles isolated inside Shadow DOM, and a script of your own fills the worksheet once the editor is ready.

Install the Package

Install TXTextControl.TXSheetControl.AspNetCore 2.0.0 from the Text Control Private NuGet Feed; Install the NuGet Packages shows how. The package targets .NET 10 and provides the complete browser editor and the static fonts, independently of the Server package.

Configure the Application

Configure the services and the request pipeline in the application's startup code:

using TXTextControl.TXSheetControl.AspNetCore;

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseStaticWebAssets();
builder.Services.AddRazorPages();

var app = builder.Build();
app.UseTXSheetControlAssets();
app.UseStaticFiles();
app.MapRazorPages();
app.Run();

The code works as follows:

  • UseStaticWebAssets enables static web assets. The font and language assets of the editor are part of the package's static web assets.
  • AddRazorPages and MapRazorPages add Razor Pages and map their endpoints.
  • UseTXSheetControlAssets rejects private and debugging paths before the static-file or static-web-asset endpoint middleware executes. Call it before UseStaticFiles, as the code does.
  • UseStaticFiles serves the static files of the application, such as wwwroot/sales.js.

Register the Tag Helper

In Pages/_ViewImports.cshtml:

@using TXTextControl.TXSheetControl.AspNetCore
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, TXTextControl.TXSheetControl.AspNetCore

The last line registers TXSheetControlTagHelper, the Tag Helper of the package, which renders a browser editor for the tx-sheet-control element.

Add the Editor to a Razor Page

Add the editor to a Razor page:

@page
@{
    var options = new TXSheetControlOptions { License = Environment.GetEnvironmentVariable("TX_SHEET_LICENSE") };
}

<tx-sheet-control id="sales" height="640" options="options" />

<script src="~/sales.js"></script>

The tx-sheet-control element renders the editor with the ID sales and a Height of 640. The script element loads sales.js, which the next section creates.

Important

Stylesheet and Scripts. The Tag Helper automatically includes its browser script and initializer. The editor installs its bundled stylesheet inside its shadow root. Do not add those package references manually.

The Tag Helper emits the assets once per request, including on pages with multiple editors. Their URLs honor virtual application paths and static-file cache versions.

Fill the Worksheet From JavaScript

In wwwroot/sales.js:

document.addEventListener('txsheetcontrolready', event => {
    if (!(event instanceof CustomEvent) || !(event.target instanceof HTMLElement) || event.target.id !== 'sales') return;

    const sheet = event.detail.workbook.sheets.at(0);
    sheet.range('A1:B3').setValues([
        ['Item', 'Amount'], ['Hardware', 120], ['Software', 80]
    ]);

    sheet.cell('B4').setFormula('=SUM(B2:B3)');
});

The code works as follows:

  • The handler listens on document for the txsheetcontrolready event of the editor.
  • It returns unless the event is a CustomEvent whose target is the element with the ID sales, so it handles this one editor only.
  • event.detail is the editor. sheets and at return the first worksheet of its workbook.
  • range and setValues write the headings Item and Amount and two items into A1:B3.
  • cell and setFormula put the formula =SUM(B2:B3) into B4.

After initialization, use event.detail or TXTextControl.TXSheetControl.getControl('sales') to reach the editor; getControl takes the ID or the element. Subscribe to txsheetcontrolerror for initialization failures.

Important

Register Handlers Early. Register ready handlers during page loading, before initialization completes.

Pass Options to the Editor

Pass TXSheetControlOptions through the options attribute, the Options property of the Tag Helper. They set:

  • The locale with Locale.
  • The theme with Theme.
  • The visible control areas with Areas.
  • The dashboard viewer mode with DashboardMode. The viewer permits dashboard filters and drill-down without worksheet navigation or authoring.

How Fonts and Assets Are Deployed

The default fonts use the packaged static catalog. No processing-server or font-service registration is required. A normal build and publish retain the editor assets.

Try OpenDocument Workbooks in the Razor Sample

The repository’s Razor workbook sample opens .xlsx, .ods and .txwb files. Choose Save ODS to download an OpenDocument spreadsheet with optional document properties and original-package preservation. The Pivot Tables and Barcodes samples also offer Save ODS, and the mail-merge sample includes ODS in Download format. The server PDF sample can load its ODS invoice template before merging and rendering the invoice.

These operations use the same load and save API as XLSX, with StreamType.OpenDocumentSpreadsheet selecting ODS.

Next Steps

Set TX_SHEET_LICENSE to the contents of your license file, tx-sheet-control.license, before starting the application. The Razor page supplies it through TXSheetControlOptions.License.