.NET
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:
- The ServerSheetControl constructor creates a control that owns an independent workbook. With
await using, DisposeAsync releases the document and its resources when the control goes out of scope. - LoadAsync loads
sales.xlsx.StreamType.SpreadsheetML, a value of StreamType, is the XLSX format. - Workbook, Sheets and AtAsync return the first worksheet.
- Cell returns the cells D2 and E2, and SetValueAsync writes the literal values 12 and 19.5 into them.
- SaveAsync exports the workbook to
sales.pdfasStreamType.PDF. Its SaveOptions hold PdfOptions in the Pdf property, with the Localeen-USand the TimeZoneUTC. - LastPdfPageCount is the number of pages of the exported PDF, which the program writes to the console.
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?[][]
{
[], ["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
ItemandAmountand two items into it, one array per row. - SetFormulaAsync writes the formula
=SUM(B2:B3)into B4. The leading equals sign is required. SaveAsyncsaves the workbook asreport.xlsxin 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 aJsonElementrepresenting 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
- Backend Services: build a request flow that loads a template, applies data and returns the bytes.
- Lifetime and Concurrency: plan document ownership, sessions and concurrent jobs.
- Mail Merge With JSON: fill workbook templates from JSON records.
- .NET Server Applications: build, publish and deploy the complete application output.
- Loading and Saving Workbooks: choose the stream type for XLSX, TXWB, PDF, CSV, XML and JSON.