Delphi的Excel自动化的Excel获取文本和价值

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

刚刚我有一个函数(假设工作簿已经打开)从工作表中select一个范围,并使用.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中呈现给用户。 我不认为你可以在整个范围内执行操作。 我过去的做法就是这样。 请注意,我正在使用早期绑定的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上提到的第一个问题出来的: 如何在Excel中读取单元格的格式化文本表示

也许:

 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; 

你可以在这里find许多很多的例子:

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