【问题标题】:Getting the text and not value with Delphi Ole Automation of Excel使用 Excel 的 Delphi Ole 自动化获取文本而不是值
【发布时间】:2013-02-20 05:14:30
【问题描述】:

我希望使用 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;

我希望我可以将行更改为 Result := ARange.Text 但它返回一个空对象。

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

【问题讨论】:

    标签: excel delphi automation delphi-7 excel-2010


    【解决方案1】:

    我从您的问题推断出您想要阅读 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;
    

    事实上,这段代码来自我在 Stack Overflow 上提出的第一个问题:How do I read the formatted textual representation of a cell in Excel

    【讨论】:

    • +1,但是阅读您的最后一句话,问题实际上是重复的吗? :-D
    【解决方案2】:

    也许:

    var
    WS:tExcelWorksheet;
    icolor:integer;
    dato:olevariant;
    

    ...

    icolor:=WS.cells.item[1,1].Interior.Color;
    dato:=WS.Cells.item[2, 4].value;
    dato:=WS.Cells.item[2, 4].textformat;
    

    你可以在这里找到很多很多例子:

    http://delphimagic.blogspot.com.es/2013/03/funciones-excel.html http://delphimagic.blogspot.com.es/2013/03/trabajar-con-graficos-en-excel-con.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多