TX Sheet Control

.NET

Topic

ServerSheetControl loads, edits, calculates and saves workbooks in a .NET application, without a browser. Two short programs cover the basics: the first changes two cells of an XLSX file and exports it as PDF, the second creates a workbook with a formula and saves it as XLSX.

Set TX_SHEET_LICENSE to the contents of your license file, tx-sheet-control.license, which the Text Control Account issues for your Trial, Developer or OEM serial number, before running the examples. The server reads this environment setting when the explicit license option is omitted.

Install the Package

Install TXTextControl.TXSheetControl.Server 2.0.0 into a .NET 10 or newer application; Install the NuGet Packages shows how to install it from the Text Control Private NuGet Feed or the nuget folder of the download. No browser editor package is required.

The package copies its required processing assets and fonts into both the build and publish output.

Warning

Deploy the Complete Output. Deploy the complete output, rather than selecting only the main assembly.

Convert an XLSX File to PDF

The following program loads sales.xlsx, writes two values and exports the workbook as sales.pdf. Sample Documents offers a sales spreadsheet named sales.xlsx to try it with.

using TXTextControl.TXSheetControl.Server;

await using var control = new ServerSheetControl();
await control.LoadAsync("sales.xlsx", StreamType.SpreadsheetML);

var sheet = await control.Workbook.Sheets.AtAsync(0);
await sheet.Cell("D2").SetValueAsync(12);
await sheet.Cell("E2").SetValueAsync(19.5);
await control.SaveAsync("sales.pdf", StreamType.PDF, new SaveOptions
{
    Pdf = new PdfOptions { Locale = "en-US", TimeZone = "UTC" }
});

Console.WriteLine($"Saved {control.LastPdfPageCount} pages.");

The code works as follows:

Set TX_SHEET_LICENSE to the contents of your license file before running the program. Developer and OEM server exports have no licensing notices; Trial exports carry trial branding.

Create a Workbook

The following program creates a workbook from scratch: it fills a range, adds a formula and saves the result as report.xlsx.

await using var control = new ServerSheetControl();
var sheet = await control.Workbook.Sheets.AtAsync(0);
await sheet.Range("A1:B3").SetValuesAsync(new object?[][]
{
    ["Item", "Amount"], ["Hardware", 120], ["Software", 80]
});

await sheet.Cell("B4").SetFormulaAsync("=SUM(B2:B3)");
await control.SaveAsync("report.xlsx", StreamType.SpreadsheetML);

The code works as follows:

  • Range returns the range A1:B3, and SetValuesAsync writes the headings Item and Amount and two items into it, one array per row.
  • SetFormulaAsync writes the formula =SUM(B2:B3) into B4. The leading equals sign is required.
  • SaveAsync saves the workbook as report.xlsx in the XLSX format, StreamType.SpreadsheetML.

How Addresses and Values Work

  • Cell addresses. A1 addresses identify cells, such as "B4", and ranges, such as "A1:B3".
  • Indices. Collection indices are zero-based: AtAsync(0) returns the first worksheet.
  • Reading values. GetValueAsync reads the calculated value of a cell: GetValueAsync() returns a JsonElement representing the calculated scalar. Use an accessor appropriate to its actual value kind.
  • Numbers. Supply numeric values rather than localized number strings.

Handle Requests

For a control that serves requests:

  • Awaiting. Await every operation before you use its result or dispose its owner.
  • Separate jobs. Give independent jobs separate documents or controls, or explicitly managed sessions.
  • Reuse. Reuse can reduce initialization costs, but reload the original input for unrelated requests.
  • Cancellation and failures. Propagate cancellation tokens, inspect import warnings and report failures, rather than treating canceled exports as success.

Next Steps