使用Delphi Ole Automation of Excel获取文本而不是价值

sxt*_*son 1 delphi excel automation delphi-7 excel-2010

我想在Delphi 7中使用Ole Automation在Excel电子表格中提取给定范围内每个单元格的文本.

刚才我有一个函数(假设工作簿已经打开)从工作表中选择一个范围并使用.Value2函数填充Variant数组

function GetExcelRange(const AWorkbookIndex: integer; const AWorksheetIndex: integer; const firstCol, lastCol, firstRow, lastRow: integer): Variant;
var
 AWorkbook, ARange: OleVariant;
begin
 Result := Unassigned;
 if not VarIsEmpty(ExcelApp) then
 begin
  if ExcelApp.Workbooks.Count >= AWorkbookIndex then
  begin
   AWorkbook := ExcelApp.Workbooks[AWorkbookIndex];
   try
    if AWorkbook.Worksheets.Count >= AWorksheetIndex then
    begin;
     ARange := AWorkbook.WorkSheets[AWorksheetIndex].Range[AWorkbook.WorkSheets[AWorksheetIndex].Cells[firstRow, firstCol],
                                   AWorkbook.WorkSheets[AWorksheetIndex].Cells[lastRow, lastCol]];
     Result := ARange.Value2;
    end;
   finally
    AWorkbook := Unassigned;
    ARange := Unassigned;
   end;
  end;
 end;
end;
Run Code Online (Sandbox Code Playgroud)

我期望的是我可以改变行,Result := ARange.Text但它返回一个空对象.

我宁愿不在Ole对象处于活动状态时迭代每个单元格,并将整个范围的文本粘贴到一个数组中,就像我上面所做的那样.

Dav*_*nan 5

我从您的问题中推断出您想要读取在Excel中呈现给用户的单元格的文本内容.我不认为你可以在整个范围内执行操作.我过去做过的方式是这样的.请注意,我正在使用早期绑定的COM.

function GetCellVariant(const Sheet: ExcelWorksheet; const Row, Col: Integer): OleVariant;

  function ErrorText(const Cell: ExcelRange; hr: HRESULT): string;
  const
    ErrorBase=HRESULT($800A0000);
  var
    i: Integer;
  begin
    Result := Cell.Text;
    for i := 1 to Length(Result) do begin
      if Result[i]<>'#' then begin
        exit;
      end;
    end;
    if hr=ErrorBase or xlErrDiv0 then begin
      Result := '#DIV/0!';
    end else if hr=ErrorBase or xlErrNA then begin
      Result := '#N/A';
    end else if hr=ErrorBase or xlErrName then begin
      Result := '#NAME?';
    end else if hr=ErrorBase or xlErrNull then begin
      Result := '#NULL!';
    end else if hr=ErrorBase or xlErrNum then begin
      Result := '#NUM!';
    end else if hr=ErrorBase or xlErrRef then begin
      Result := '#REF!';
    end else if hr=ErrorBase or xlErrValue then begin
      Result := '#VALUE!';
    end else begin
      Result := 'an error';
    end;
  end;

var
  Cell: ExcelRange;
  hr: HRESULT;
begin
  Cell := GetCellAsRange(Sheet, Row, Col);//effectively this is Sheet.Range
  if VarIsError(Cell.Value, hr) then begin
    raise ECellValueError.CreateFmt(
      'Cell %s contains %s.',
      [R1C1toA1(Row,Col), ErrorText(Cell, hr)]
    );
  end;
  Result := Cell.Value;
end;

function GetCellString(const Sheet: ExcelWorksheet; const Row, Col: Integer): string;
var
  Value: Variant;
  Cell: ExcelRange;
begin
  Value := GetCellVariant(Sheet, Row, Col);
  if VarIsNumeric(Value) then begin
    Cell := GetCellAsRange(Sheet, Row, Col);
    Result := Sheet.Application.WorksheetFunction.Text(Cell.Value, Cell.NumberFormatLocal);
  end else begin
    Result := ConvertToString(Value);//this converts a Variant to string
  end;
end;
Run Code Online (Sandbox Code Playgroud)

事实上,这段代码来自我在Stack Overflow上提出的第一个问题:如何在Excel中读取单元格的格式化文本表示