TXLSWorkbookViewer Component
TXLSWorkbookViewer is a VCL control that displays worksheets from either workbook engine without Microsoft Excel or COM automation
Workbook sources
- Assign
Workbookto display an existingTXLSWorkbook - Assign
XlsxWorkbookto display an existingTXLSXWorkbook - Call
LoadFromFileto let the viewer own and load an XLS, XLSX, XLSM, XLTX, XLTM, or ODS workbook - Call
ClearWorkbookbefore freeing an externally owned workbook that is still attached
Navigation
The control provides horizontal and vertical scrollbars, mouse-wheel scrolling, mouse dragging, Shift range extension, arrow keys, Home, Page Up, Page Down, and Tab navigation
GoToCell selects a row and column, SelectRange selects a rectangle, NextWorksheet and PreviousWorksheet switch sheets, and SelectedRange exposes the normalized A1 reference
Editing and calculation
Set Editable to True to allow F2, double-click, printable-key, Delete, and formula-bar edits; locked cells on a protected worksheet remain read only
BeginEdit, CommitEdit, and CancelEdit control the in-place editor, while EditSelectedCell and FormulaBarText support an external formula bar
Committed edits recognize formulas, booleans, integers, floating-point values, text, and blank input, then recalculate dependent formulas and invalidate conditional-format and pagination caches
Undo and Redo use an O(1) bounded circular history; set UndoLimit to control capacity or to zero to disable history
Delete or ClearSelectedRange clears the selection as one transaction; Ctrl+C and CopySelectedRange copy Unicode tab-separated text, while Ctrl+V, PasteSelectedRange, and PasteText paste a rectangular text block at the selection's top-left corner with one undo entry
Protection and structural preflight rejects an entire blocked range before mutation; undo and redo preserve native rich text, formula caches, styles, and absent cells in both models, with the same protection check on every history attempt
Existing array formulas, data-table regions, and multi-cell pastes intersecting a merge are rejected; single merged-anchor edits and whole-merge clears preserve merge geometry, while partial merged-range clears are rejected
The shared TXLSWorkbookEditHistory engine can also be instantiated without a control; see headless editing transactions for coordinate, lifetime, calculation, and operation-limit rules
Formula reference selection
Call BeginReferencePick while a host formula editor is collecting a worksheet reference; each selection change raises OnReferencePick with the current A1 range until EndReferencePick is called
Rendering
The viewer uses each worksheet's column widths, row heights, and formatted display text, renders classic solid fills and font styling, and provides configurable headers, gridlines, and selection colors
For XLSX worksheets, the viewer also renders conditional fills, font colors and emphasis, number formats, color scales, directional positive and negative data bars, gradients, borders, axes, hidden values, and all built-in icon-set families
The viewer caches one prepared conditional-format evaluator for the active worksheet so scrolling and selection repaints do not rescan the workbook; call RefreshConditionalFormats after changing values, formulas, ranges, or conditional rules in an attached workbook
XLSX rendering checks sparse cells and interval style overlays without indexing empty coordinates, so scrolling does not materialize visible blank cells or expand worksheet storage
Print preview
Set PrintPreview to True to display the active worksheet through the shared TXLSPaginationModel used by PDF page geometry and counting
PreviewPage is one based and clamps to PreviewPageCount; the arrow, Page Up, Page Down, Home, End, mouse-wheel, and vertical-scroll commands navigate logical pages while preview is active
PaginationModel exposes the cached model, SavePreviewPageAsMetafile writes the selected logical page as EMF, and RefreshPagination rebuilds the cache after changes to values, dimensions, print settings, breaks, titles, or drawings
Events
OnSelectionChangereports the newly selected row and columnOnWorksheetChangefires after the active worksheet changesOnBeforeEditcan alter the proposed input or reject editingOnAfterEditreports a committed cell editOnReferencePickpublishes the selected A1 range in reference-pick mode
Delphi example
Viewer := TXLSWorkbookViewer.Create(Self);
Viewer.Parent := Self;
Viewer.Align := alClient;
Viewer.XlsxWorkbook := Workbook;
Viewer.Editable := True;
Viewer.SelectRange(2, 1, 5, 3);
Caption := Viewer.SelectedRange;
Viewer.GoToCell(2, 3);
Viewer.FormulaBarText := '=A1*2';
Viewer.Undo;
Viewer.Redo;
Viewer.RefreshConditionalFormats;
Viewer.PrintPreview := True;
Viewer.PreviewPage := 1;
Viewer.SavePreviewPageAsMetafile('Preview.emf');
Viewer.GoToCell(12, 4);
Caption := Viewer.WorksheetName + ': ' + Viewer.SelectedCellText;
See Demo\Delphi\WorkbookViewer for a complete run-time-created viewer with file opening and worksheet controls