ExcelApp: Variant和ExcelApp:=CreateOleObject('Excel.application')这两句话一定要写在同一个过程里吗

  • 主题发起人 主题发起人 shanshengsheng
  • 开始时间 开始时间
S

shanshengsheng

Unregistered / Unconfirmed
GUEST, unregistred user!
ExcelApp: Variant和ExcelApp:=CreateOleObject('Excel.application')这两句话一定要写在同一个过程里吗? ( 积分: 100 )<br />ExcelApp: Variant和ExcelApp:=CreateOleObject('Excel.application')这两句话一定要写在同一个过程里吗?

为什么定义和创建写在一起呢?

我没有写在一起,编译没有错,运行提示无效的Variant类型;
附程序代码:
unit ExcelObj;

interface

uses
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;
 
ExcelApp: Variant和ExcelApp:=CreateOleObject('Excel.application')这两句话一定要写在同一个过程里吗?

为什么定义和创建写在一起呢?

我没有写在一起,编译没有错,运行提示无效的Variant类型;
附程序代码:
unit ExcelObj;

interface

uses
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;
 
已搞定,是调用错误
分给第一个人
 
呵呵,给出谁呀?

http://delphi.ktop.com.tw这是一个很好的网站,我常去那里找答案
 
后退
顶部