HotXLS Docs

WriteCells method

Fills a classic XLS worksheet range by calling application code for each cell value

Syntax

type
  TXLSCellWriteEvent = procedure(
    Sender: TObject;
    SheetIndex, Row, Col: Integer;
    var Value: Variant;
    var Skip: Boolean;
    var Cancel: Boolean) of object;

function WriteCells(ARange: WideString; Callback: TXLSCellWriteEvent): Integer; overload;
function WriteCells(ARow1, ACol1, ARow2, ACol2: Integer; Callback: TXLSCellWriteEvent): Integer; overload;

Remarks

The string overload accepts an A1-style range such as A1:C100. The numeric overload uses 1-based row and column indexes. Ranges are normalised when the start row or column is greater than the end row or column
Cells are requested in row-major order. Set Value in the callback to write the current cell. Set Skip to True to leave the current cell unchanged. Set Cancel to True to stop after the current callback. The return value is the number of cells written
This method writes into the loaded worksheet model. It is a compact callback-based writing helper, not a file-streaming save mode

Example

procedure TForm1.WriteCell(Sender: TObject; SheetIndex, Row, Col: Integer;
  var Value: Variant; var Skip: Boolean; var Cancel: Boolean);
begin
  if Col = 3 then
  begin
    Skip := True;
    Exit;
  end;

  Value := Format('R%dC%d', [Row, Col]);
  if Row >= 1000 then
    Cancel := True;
end;

Written := Workbook.Sheets[1].WriteCells('A1:D2000', WriteCell);

Typed row and column writers

TXLSWorksheet and TXLSXWorksheet also accept open numeric, string, and variant arrays without a per-cell callback
function WriteFloatRowValues(ARow, ACol: Integer; const Values: array of Double): Integer;
function WriteStringRowValues(ARow, ACol: Integer; const Values: array of WideString): Integer;
function WriteRowValues(ARow, ACol: Integer; const Values: array of Variant): Integer;
function WriteFloatColumnValues(ARow, ACol: Integer; const Values: array of Double): Integer;
function WriteStringColumnValues(ARow, ACol: Integer; const Values: array of WideString): Integer;
function WriteColumnValues(ARow, ACol: Integer; const Values: array of Variant): Integer;
Coordinates are one-based. Values continue right for row writers and downward for column writers. Writes are clipped at the active file format's row or column limit, invalid starting coordinates return zero, and the result is the number of values written. XLSX writers grow collection capacity geometrically across repeated batches
Revenue[0] := 1250.0;
Revenue[1] := 980.5;
Revenue[2] := 1425.25;
Written := Sheet.WriteFloatColumnValues(2, 4, Revenue);

See Also