2013-03-05 130 views
1

我期待提取每個單元格的文本在給定的範圍在Delphi使用OLE自動化7.掌握的Excel

剛纔我有一個Excel電子表格德爾福OLE自動化的文字,而不是價值功能(假設該工作簿已經打開),從工作表中選擇一個範圍,並填充使用.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對象處於活動狀態時,我寧願不迭代每個單元格,並將整個範圍的文本粘貼到數組中,就像我在上面做的那樣。

回答

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; 

其實這段代碼就出來了我曾經問這裏對堆棧溢出的第一個問題:How do I read the formatted textual representation of a cell in Excel

+1

+1,但是讀完你的最後一句話,這個問題實際上是重複的嗎? :-D – 2013-03-06 07:14:49