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.
附程序代码:
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.
解决方案 »
- 在打开窗体时提示:TOrasession类没有找到
- delphi2009的IDE能否自定义各个工具箱面板的位置?
- 怎样获取sql数据服务器名的列表?
- 关于IE Bar 菜单问题
- delphi6中的控件TWordApplication等等为何都没有帮助?应该哪里找帮助?
- 不是不把FORM关闭,就可以释放所有在这个FORM上的资源
- 一个简单的菜鸟问题,救命!300分
- 请帮忙看看这段程序,急急!!
- 请问哪里有Delphi的Update1、Update2#下载?
- 如何使用Office相关的那些控件
- 请问怎样监视 ADOConnection 是否连接!!
- 用nmudp控件接收数据(字符串)时,字符串被"0"截断,请问高手如何解决?
不过就是缺少判断,如果文件不存在怎么办,如果前一步创建失败怎么办?
如果我把定义的语句放到create过程里就没有问题了。