ExcelApp: Variant和ExcelApp:=CreateOleObject('Excel.application')这两句话一定要写在同一个过程里吗?为什么定义和创建写在一起呢?我没有写在一起,编译没有错,运行提示无效的Variant类型;
附程序代码:
unit ExcelObj;interfaceuses
  Windows, Excel97,Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,comobj,OleServer,ComCtrls,OleCtrls;type
  TExcelObj = class(TObject)
  private
    ExcelApp: Variant;
    RowStart: Integer;
    sheet1: Variant;
    WorkBook: Variant;
    function GetCount: Integer;
  public
    constructor Create; overload;
    constructor Create(FilePath,FieldsName:string;startrow:integer); overload;
    destructor Destroy; override;
    property Count: Integer read GetCount;
  end;
  implementation{
********************************** TExcelObj ***********************************
}
constructor TExcelObj.Create;
begin
  inherited Create;
end;constructor TExcelObj.Create(FilePath,FieldsName:string;startrow:integer);
begin
  rowstart:=startrow;
  ExcelApp:=CreateOleObject('Excel.application');
  WorkBook:=CreateOleobject('Excel.Sheet');
  ExcelApp.visible:=false;
  WorkBook:=ExcelApp.workBooks.Open(FilePath);
  sheet1:=WorkBook.worksheets[1];
end;destructor TExcelObj.Destroy;
begin
  ExcelApp.Quit;
  ExcelApp:=Unassigned;
  inherited Destroy;
end;function TExcelObj.GetCount: Integer;
var
  k: Integer;
begin
  k:=Sheet1.usedRange.Cells.rows.count;
  result:=k-rowstart;
end;end.