近来看了一些面向对象的资料,就练习写了一个excel读取的类,还有很多不完善的地方,不知道该怎么修改了,贴出来看看谁能给点意见
unit ExcelObj;
//*****************************************************************************
// 调用示例:excelobj:=TExcelobj.create(FilePath,'id,name,',startrow);
// excelobj.fields[0].asstring;
// excelobj.next;
//
//
//
//
//*****************************************************************************
interface
uses
Windows, Excel97,Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,comobj,OleServer,ComCtrls,OleCtrls;
type
TExField = class(TComponent)
private
FValue: string;
function GetAsString: string;
procedure SetAsString(const Value: string);
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
property AsString: string read GetAsString write SetAsString;
end;
TExFields = class(TObject)
private
FList: TList;
function GetFields(Index: Integer): TExField;
procedure SetFields(Index: Integer; Value: TExField);
protected
FNameLst: TStringList;
public
constructor Create(FieldsLst: string);
destructor Destroy; override;
function FieldByName(const FieldName: string): TExField;
property Fields[Index: Integer]: TExField read GetFields write SetFields;
default;
end;
TExcelObj = class(TObject)
private
CurRow: Integer;
ExcelApp: Variant;
FFields: TExFields;
FhaveExcel: Boolean;
RowStart: Integer;
sheet1: Variant;
WorkBook: Variant;
function GetEof: Boolean;
function GetRecordCount: Integer;
procedure SetValue;
public
constructor Create; overload;
constructor Create(FilePath, FieldsNameLst: string; startrow: integer);
overload;
destructor Destroy; override;
function FieldByName(FieldName: string): TExField;
procedure Next;
property Eof: Boolean read GetEof;
property Fields: TExFields read FFields;
property haveExcel: Boolean read FhaveExcel;
property RecordCount: Integer read GetRecordCount;
end;
implementation
{
*********************************** TExField ***********************************
}
constructor TExField.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
end;
destructor TExField.Destroy;
begin
inherited Destroy;
end;
function TExField.GetAsString: string;
begin
result:=FValue;
end;
procedure TExField.SetAsString(const Value: string);
begin
FValue:=Value;
end;
{
********************************** TExFields ***********************************
}
constructor TExFields.Create(FieldsLst: string);
var
i: Integer;
strtemp: string;
Field: TExField;
begin
FList := TList.Create;
FNameLst := TStringList.Create;
i:=0;
repeat
i:=pos(',',FieldsLst);
if i<>0 then
begin
strtemp:=copy(FieldsLst,1,i-1);
FNameLst.Add(strtemp);
FieldsLst:=copy(FieldsLst,i+1,length(FieldsLst)-i);
field:=TExField.Create(nil);
flist.Add(Field);
end;
until i=0;
end;
destructor TExFields.Destroy;
begin
if FList <> nil then FList.Clear;
FList.Free;
if FNameLst <> nil then FNameLst.Clear;
FNameLst.Free;
inherited Destroy;
end;
function TExFields.FieldByName(const FieldName: string): TExField;
var
i, cnt: Integer;
begin
for i:=0 to FNameLst.Count-1 do
begin
Result:= FList.Items[i];
if FieldName=FNameLst.Strings[i] then exit;
end;
Result := nil;
end;
function TExFields.GetFields(Index: Integer): TExField;
begin
Result:=FList.Items[Index];
end;
procedure TExFields.SetFields(Index: Integer; Value: TExField);
begin
end;
{
********************************** TExcelObj ***********************************
}
constructor TExcelObj.Create;
begin
inherited Create;
end;
constructor TExcelObj.Create(FilePath, FieldsNameLst: string; startrow:
integer);
begin
inherited Create;
FhaveExcel:=false;
try
ExcelApp:=CreateOleObject('Excel.application');
WorkBook:=CreateOleobject('Excel.Sheet');
ExcelApp.visible:=false;
FhaveExcel:=true;
except
end;
if not FhaveExcel then exit;
rowstart:=startrow;
currow:=startrow;
FFields:=TExFields.Create(FieldsNameLst);
WorkBook:=ExcelApp.workBooks.Open(FilePath);
sheet1:=WorkBook.worksheets[1];
SetValue;
end;
destructor TExcelObj.Destroy;
begin
if FhaveExcel then
begin
ExcelApp.Quit;
ExcelApp:=Unassigned;
FFields.Free;
end;
inherited Destroy;
end;
function TExcelObj.FieldByName(FieldName: string): TExField;
begin
result:=Fields.FieldByName(FieldName);
end;
function TExcelObj.GetEof: Boolean;
begin
if CurRow>RecordCount then
Result:=true
else
Result:=false;
end;
function TExcelObj.GetRecordCount: Integer;
var
k: Integer;
begin
k:=Sheet1.usedRange.Cells.rows.count;
result:=k-rowstart+1;
end;
procedure TExcelObj.Next;
begin
inc(currow);
SetValue;
end;
procedure TExcelObj.SetValue;
var
i, cnt: Integer;
begin
cnt:=FFields.FNameLst.Count;
for i:=1 to cnt do
begin
Fields[i-1].AsString:=sheet1.cells.item[CurRow+RowStart-1,i];
end;
end;
end.