Loading and Saving Workbooks
TX Sheet Control loads and saves workbooks through a public dispatcher, load and save, and a stream type that selects the file format. Use this dispatcher rather than calling undocumented format helpers. A filename extension alone is not the API's format selector.
What follows covers the seven formats, loading and saving in the browser editor, string output for tabular data, the delivery of the saved bytes and the handling of options and failures.
Supported Formats
| Format | Stream type | Read | Write | Typical use |
|---|---|---|---|---|
| XLSX | SpreadsheetML | Yes | Yes | Exchange spreadsheet documents with Excel-compatible applications |
| ODS | OpenDocumentSpreadsheet | Yes | Yes | Exchange OpenDocument workbooks and retain original package content |
| TXWB | InternalWorkbookFormat | Yes | Yes | Retain TX Sheet Control views, dashboards and report metadata |
PDF | No | Yes | Deliver fixed-page output | |
| CSV | CSV | Yes | Yes | Exchange one worksheet as UTF-8 delimited values |
| XML | XML | Yes | Yes | Exchange tabular rows and scalar fields |
| JSON | JSON | Yes | Yes | Exchange record arrays or positional row arrays |
The stream types are the values of the StreamType enumeration. The CSV, XML and JSON APIs transfer values, not full workbook features. CSV, XML and JSON Data has complete examples, the supported schemas and the import and export settings of these three formats.
Load and Save a Workbook
The following code loads an XLSX file into the browser editor and saves the workbook as XLSX, TXWB and PDF. editor is the Spreadsheet of the browser editor, created as in JavaScript, and file is the XLSX file to load, such as an upload:
const bytes = new Uint8Array(await file.arrayBuffer());
const result = await editor.load(bytes, StreamType.SpreadsheetML);
for (const warning of result.warnings) console.warn(warning);
const xlsx = await editor.save(StreamType.SpreadsheetML);
const txwb = await editor.save(StreamType.InternalWorkbookFormat);
const pdf = await editor.save(StreamType.PDF);
The code works as follows:
- load reads the bytes as XLSX, which
StreamType.SpreadsheetMLselects, and returns a LoadResult. Itswarningsare the import notices of the conversion, which the loop writes to the console. - save returns the bytes of the workbook in the format of the stream type: XLSX, TXWB or PDF.
TXWB is a workbook archive, not an arbitrary JSON file. Loading raw JSON under the InternalWorkbookFormat stream type is rejected.
Important
Handles After Loading. Loading replaces the document and invalidates old handles. Reacquire sheets and ranges after a successful load.
OpenDocument Spreadsheet
Use StreamType.OpenDocumentSpreadsheet with the same byte-based load and save methods in JavaScript, TypeScript, Angular, ASP.NET Core, Node.js and .NET. OpenDocument Spreadsheet explains preservation, format settings and examples.
Save CSV, XML or JSON as a String
For CSV, XML and JSON, you can get a string directly instead of bytes: call await editor.saveText(StreamType.CSV, options) with saveText in JavaScript, or await control.SaveTextAsync(StreamType.CSV, options) with SaveTextAsync in .NET.
The worksheet and range settings, cancellation, save events and UTF-8 byte limits are the same as for byte export. A generated CSV BOM is omitted from strings.
Save Tabular Content as a String shows both methods with their settings.
Deliver the Bytes
How the saved bytes reach their destination depends on where your code runs:
- In a browser, create a
Blobwith the appropriate MIME type and offer it through an object URL or a download link, then revoke the URL. - In .NET, use the file, stream or byte overloads of SaveAsync.
- In Node.js, write the returned bytes or send them through an authorized response.
File dialogs, storage names and access permissions are the responsibilities of your application.
Set Options and Handle Failures
Use the LoadOptions and SaveOptions of your package for size limits, format-specific settings, metadata and cancellation, for example LoadOptions and SaveOptions in the browser editor.
Inspect the warnings and handle rejected tasks and promises. Do not show a success message before the bytes are available. A failed or canceled operation does not emit a successful loaded or saved notification.
Next Steps
- CSV, XML and JSON Data: exchange the values of one worksheet with other applications.
- XLSX Compatibility: check how an Excel workbook survives loading and saving.
- Exporting Worksheets to PDF: create the fixed-page PDF output.
- Document Properties: set the metadata of the documents you save.
- Events and Cancellation: react to loading and saving, and cancel them.