HotXLS 文档

ForEachCell 方法

枚举已加载经典 XLS 工作表中的单元格,并对每个单元格调用一次应用程序回调

语法

type
  TXLSCellReadEvent = procedure(
    Sender: TObject;
    SheetIndex, Row, Col: Integer;
    const Value: Variant;
    const Formula: WideString;
    var Cancel: Boolean) of object;

function ForEachCell(Callback: TXLSCellReadEvent): Integer;

备注

ForEachCell 只遍历已加载工作表模型中存在的单元格,不会扫描工作表中每一个可能的行和列
Cells are visited in row-major order. SheetIndex, Row, and Col are 1-based public coordinates. Value is the current cell value. Formula 保存公式单元格的公式文本,对普通值单元格则为空
Set Cancel to True 即可在回调中提前终止扫描。返回值是回调被调用的次数

示例

procedure TForm1.ReadCell(Sender: TObject; SheetIndex, Row, Col: Integer;
  const Value: Variant; const Formula: WideString;
  var Cancel: Boolean);
begin
  Memo1.Lines.Add(Format('%d:%d = %s', [Row, Col, VarToStr(Value)]));
  if Row >= 1000 then
    Cancel := True;
end;

Visited := Workbook.Sheets[1].ForEachCell(ReadCell);

See Also