2013-03-05 110 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