HotXLS Docs

Large Spreadsheet Streaming

Enterprise edition: Everything described here - the streaming reader, row cursor and writer - is available only in HotXLS Enterprise (HotXLS-ENT / HotXLS-DCU-ENT), in the HotXLSEnterprise runtime package for RAD Studio and HotXLSLazEnterprise.lpk for Lazarus (demo: Demo\Delphi\DirectStreaming); the trial build includes it under the usual trial limits

Overview

Pulling very large spreadsheets (say millions of populated cells) into memory can trigger out-of-memory errors in 32-bit processes; HotXLS ships streaming readers and writers that hold near-constant memory and skip DOM generation while parsing a workbook

Direct Reader API

The TXLSDirectReader class parses workbook streams sequentially without creating in-memory cell models; it fires events as cells are encountered TXLSDirectReader class parses workbook streams without creating in-memory cell models and fires OnSheet and OnCell events as selected cells are encountered

Reader := TXLSDirectReader.Create;
try
  Reader.ParallelSheets := True;
  Reader.ParallelMaxThreads := 4;
  Reader.ParallelBufferMemoryLimit := 16 * 1024 * 1024;
  Reader.OnDimension := HandleDimension;
  Reader.OnColumn := HandleColumn;
  Reader.OnRow := HandleRow;
  Reader.OnMerge := HandleMerge;
  Reader.OnCell := HandleCell;
  Reader.ReadFile('large.xlsx');
finally
  Reader.Free;
end;

ParallelSheets switches on worker-local worksheet inflaters and XML readers, with the calling thread emitting callbacks in worksheet and cell order

ParallelBufferMemoryLimit caps the aggregate queued raw-cell payload across active workers and defaults to 16 MiB, whereas ParallelPeakBufferedBytes reports the last parallel read's peak for diagnostics and capacity tests

Every active worksheet gets its own slice of the queue budget so a later worksheet cannot starve the one being emitted; a single cell bigger than its share is admitted as irreducible payload

OnDimension, OnSheetFormat, OnColumn, OnRow, OnPane, and OnMerge expose the used range, default widths and heights, column spans, row height or visibility, outline and style indexes, pane state and merged ranges, all without constructing worksheet objects

Layout callbacks are opt-in and run inside a bounded-memory metadata pass ahead of the selected sheet's OnCell callbacks; merge ranges therefore land before cells inside the merge, even though OOXML stores mergeCells after sheetData

With parallel sheet parsing switched on, layout callbacks stay on the calling thread and finish for the selected sheets before worker queues drain

Setting Abort in OnCell This allows you to scan sheet rows sequentially and abort early when the target data boundaries are resolved

Direct Writer API

The TXLSDirectWriter class writes rows straight into the ZIP file package, which keeps memory overhead minimal when generating large data exports

Writer := TXLSDirectWriter.Create;
try
  Writer.BeginFile('large.xlsx');
  Writer.AddSheet('Data');
  Writer.AddRow(1);
  Writer.WriteString(1, 'Quarter');
  Writer.WriteString(2, 'Revenue');
  Writer.AddComment(2, 1, 'Validated source', 'Reviewer');
  Writer.AddImageFromFile(4, 1, 8, 12, 'logo.png',
    xlsDirectImagePng, 'Company logo');
  ChartIndex := Writer.AddChart(9, 1, 18, 16,
    xlsDirectChartColumn, 'Revenue');
  Writer.AddChartSeries(ChartIndex, 'Revenue',
    'Data!$A$2:$A$5', 'Data!$B$2:$B$5');
  Writer.Close;
finally
  Writer.Free;
end;

AddComment writes classic cell notes carrying author metadata plus the matching VML note shapes without loading a worksheet object model

AddImage, AddImageFromFile, and AddImageFromStream accept two-cell anchors and PNG, JPEG, GIF, BMP, EMF or WMF payloads; file sources flow straight into the ZIP entry as the sheet finishes, whereas stream sources keep only the stream reference and recorded byte range

When AddImageFromStream receives OwnsStream=True, the writer releases that stream once the worksheet side parts are finalised; otherwise the caller must keep the seekable stream alive up to the next AddSheet or Close call

AddChart and AddChartSeries generate column, bar, line, area, pie, doughnut or scatter ChartML with formula-backed series, whereas AddChartXml attaches a complete caller-supplied chart part for advanced chart families and their extensions

Comments, drawing anchors and chart descriptors are kept only for the current worksheet; once sheetData closes, the writer emits worksheet relationships, comments, VML, DrawingML, media and ChartML in sequence, then drops the side-channel state before the next worksheet starts

A long export can be stopped. OnProgress fires once every ProgressRows rows (256 by default, 0 for every row) with the work done so far - RowsWritten, the cell count, and BytesWritten in uncompressed part bytes - and setting its Cancel parameter stops the export at the next row boundary, as does calling Cancel; the write that reaches the boundary raises EXLSDirectWriteCancelled (from EXLSError), and Cancelled reports the flag so a caller can tell an abandoned export from a failure. Close will not publish an export that was cancelled, so a half-written file is never presented as a complete workbook; the target remains the caller's and may be deleted.

Deflate settings apply per entry, and the writer exposes three policy options. StoreAlreadyCompressedParts keeps media that is already compressed - PNG, JPEG, GIF, ZIP and GZIP parts - rather than deflating it a second time, since another pass cannot shrink those bytes and merely costs time; BulkPartLevel chooses the deflate level for worksheet and shared-string parts, letting a very large export request level 1 and swap a little size for much less time; PartMemLevel fixes zlib’s memLevel for each part the writer creates, exchanging compression ratio for the window held by every open stream. All three default to off or unset, so an export that asks for nothing writes precisely the bytes it always wrote.