Unit: lxHandleX
Represents a rectangular XLSX worksheet range returned by
TXLSXWorksheet.Range, RCRange,
UsedRange, EntireRow, or
EntireColumn. A range is a lightweight view over its
worksheet; it does not own cell data.
Declaration
TXLSXRange = class
function RefA1: WideString;
function SaveAsHTML(const FileName: WideString): Integer; overload;
function SaveAsHTML(Stream: TStream): Integer; overload;
property Sheet: TXLSXWorksheet;
property Row1, Col1, Row2, Col2: Integer;
property RowCount, ColCount: Integer;
procedure SetValue(const V: Variant);
procedure SetFormula(const F: WideString);
procedure Clear;
procedure ClearAll;
procedure SetFontIndex(AIdx: Integer);
procedure SetFillIndex(AIdx: Integer);
procedure SetBorderIndex(AIdx: Integer);
procedure SetNumberFormatIndex(AIdx: Integer);
procedure SetAlignmentIndex(AIdx: Integer);
procedure SetNumberFormat(const Fmt: WideString);
procedure SetBorders(Kind: TXLSXBorderEdgeKind; Style: TXLSXBorderStyle); overload;
procedure SetBorders(Kind: TXLSXBorderEdgeKind; Style: TXLSXBorderStyle; Color: LongWord); overload;
procedure SetLocked(ALocked: Boolean);
procedure SetFormulaHidden(AHidden: Boolean);
procedure AutoFitColumns;
procedure AutoFitRows;
procedure Merge; overload;
procedure Merge(Across: OleVariant); overload;
procedure Unmerge;
function Offset(DRow, DCol: Integer): TXLSXRange;
function Resize(NewRowCount, NewColCount: Integer): TXLSXRange;
end;
Members
| Member |
Description |
| RefA1 |
Returns the range reference as A1 text. Single-cell ranges return a single cell reference such as B2. |
| SaveAsHTML |
Exports only this selected range as a UTF-8 HTML table. The output uses the same escaping, basic inline styles, and merged-cell span handling as workbook-level XLSX HTML export. |
| SetValue / SetFormula |
Writes the same value or formula text to every cell in the range through the worksheet cell collection. |
| Clear / ClearAll |
Clear removes values and formulas while preserving style indexes. ClearAll also clears style indexes. |
| SetFontIndex / SetFillIndex / SetBorderIndex / SetNumberFormatIndex / SetAlignmentIndex |
Applies 1-based workbook palette indexes to each cell in the range. Pass 0 to clear the corresponding per-cell index. |
| SetNumberFormat |
Looks up or appends a number-format string in the workbook palette, then applies the resulting 1-based index to the range. An empty format clears the per-cell index. |
| SetBorders |
Composes border edges over the range. Use outline and inside edge selectors to build table borders without replacing unrelated edge settings. |
| SetLocked / SetFormulaHidden |
Applies cell-protection flags to every cell in the range by reusing the workbook protection-style pool. |
| AutoFitColumns / AutoFitRows |
Forwards to the worksheet auto-fit helpers over the range's column or row bounds. |
| Merge / Unmerge |
Merge and Merge(False) create one merged rectangle. Merge(True) creates one merged range per row, matching Excel's merge-across behavior. Unmerge removes an exact merged-range match. |
| Offset / Resize |
Returns another worksheet-owned range shifted from the current bounds or resized to the requested row and column count. |
Ownership and bounds
Ranges are owned by the worksheet that created them and are freed with that worksheet.
Application code should not call Free on a TXLSXRange
and should not keep a range reference after the worksheet or workbook is destroyed.
Numeric coordinates are 1-based. Range creation normalizes reversed
corners and clamps bounds to the XLSX worksheet limits.
Example
var
Wb: TXLSXWorkbook;
Ws: TXLSXWorksheet;
Body: TXLSXRange;
begin
Wb := TXLSXWorkbook.Create;
try
Ws := Wb.Sheets.Add('Report');
Body := Ws.Range['B2:D20'];
Body.SetNumberFormat('#,##0.00');
Body.SetBorders(xlsxEdgeOutline, xlsxBorderThin, $FF808080);
Body.AutoFitColumns;
Body.SaveAsHTML('report-fragment.html');
finally
Wb.Free;
end;
end;
See also