home *** CD-ROM | disk | FTP | other *** search
- unit UFlexCelReport;
- {******************************************************************
- Component to automate reporting to Excel
- Version 2.5
-
- // The contents of this file are subject to the Mozilla Public License
- // Version 1.1 (the "License"); you may not use this file except in compliance
- // with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
- //
- // Software distributed under the License is distributed on an "AS IS" basis,
- // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
- // specific language governing rights and limitations under the License.
- //
- // The original code is Excel.pas, released January 23, 2002.
- //
- // The initial developer of the original code is Adrian Gallero,
- // written by Adrian Gallero (agallero@netscape.net).
- //
- // Portions created by Adrian Gallero are Copyright
- // (C) 2002-2002 Adrian Gallero. All Rights Reserved.
-
- Send any comments to agallero@netscape.net
- ******************************************************************** }
- interface
- {$R IFlexcel.res}
- uses
- SysUtils, Classes, UCustomFlexCelReport, UXlsDB, UXlsTDataSet,
- contnrs, DB,
- {$IFDEF ConditionalExpressions}{$if CompilerVersion >= 14} variants,{$IFEND}{$ENDIF} //Delphi 6 or above
- typinfo, UExcelAdapter, UFlxMessages, UFlxFullDataSets;
-
- type
- TCalcRecordCount=(cr_None, cr_Count, cr_SlowCount);
- TRecordCountEvent = procedure (Sender: TObject; const DataSet: TDataSet; var RecordCount: integer) of object;
-
- TFlexCelReport = class(TCustomFlexCelReport)
- private
- FDataModule: TComponent;
- FCalcRecordCount: TCalcRecordCount;
- FPagesDataSet: TFlxDataSet;
-
- FOnRecordCount: TRecordCountEvent;
-
- procedure SetPagesDataSet(const Value: TFlxDataSet);
- procedure SetOnRecordCount(const Value: TRecordCountEvent);
- procedure SetDataModule(const Value: TComponent);
-
- function RecordCount(const DbSet: TDataSet): integer;
- { Private declarations }
-
- protected
- procedure Notification(AComponent: TComponent; Operation: TOperation); override;
- function GetDataSet(const DataSetName: string): IXlsDataSet; override;
- function GetPropertyInfo(const PropName: string): PPropInfo; override;
- function GetVariantProperty(const PropInfo: PPropInfo): variant; override;
- function GetPagesDataSet: IXlsDataSet; override;
-
- { Protected declarations }
-
- public
- constructor Create(AOwner:TComponent);override;
- destructor Destroy;override;
-
- function DesignDataModule: TComponent; override;
- { Public declarations }
-
- published
- property DataModule: TComponent read FDataModule write SetDataModule;
- property CalcRecordCount: TCalcRecordCount read FCalcRecordCount write FCalcRecordCount default cr_Count;
- property PagesDataSet: TFlxDataSet read FPagesDataSet write SetPagesDataSet;
-
- //Events
- property OnRecordCount: TRecordCountEvent read FOnRecordCount write SetOnRecordCount;
- { Published declarations }
-
- end;
-
- ClassOfTFlexCelReport= class of TFlexCelReport;
- procedure Register;
-
- implementation
-
- procedure Register;
- begin
- RegisterComponents('FlexCel', [TFlexcelReport]);
- end;
-
-
- { TFlexCelReport }
-
- constructor TFlexCelReport.Create(AOwner: TComponent);
- begin
- inherited;
- FDataModule:=AOwner;
- FCalcRecordCount:=cr_Count;
- end;
-
- destructor TFlexCelReport.Destroy;
- begin
- inherited;
- end;
-
- procedure TFlexCelReport.Notification(AComponent: TComponent; Operation: TOperation);
- begin
- inherited Notification(AComponent, Operation);
- if Operation = opRemove then
- begin
- if AComponent = FPagesDataSet then
- FPagesDataSet:= nil;
- if AComponent = FDataModule then
- FDataModule:= Owner;
- end;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////
- function TFlexCelReport.RecordCount(const DbSet: TDataSet): integer;
- begin
- Result:=0;
- if Canceled then exit;
- if DbSet<>nil then
- begin
- //Count the records
- DbSet.First;
-
- //If the event OnCountRecords is Assigned, the var FCalcRecordCount has no meaning
- if Assigned (FOnRecordCount) then FOnRecordCount(Self, DbSet, Result)
- else
- case FCalcRecordCount of
- cr_None:
- Result:= DbSet.RecordCount;
- cr_Count:
- begin
- DbSet.Last;
- DbSet.First;
- Result:= DbSet.RecordCount;
- end; //cr_Count
- cr_SlowCount:
- begin
- while not DbSet.Eof do
- begin
- inc(Result);
- DbSet.Next;
- end;
- DbSet.First;
- end; //cr_SlowCount
- end; //case
- end //DbSet <>nil
- else Result:=1;
- end;
-
- procedure TFlexCelReport.SetPagesDataSet(const Value: TFlxDataSet);
- var
- IDs: IXlsDataSet;
- begin
- if (Value<>nil) then
- if not (Value is TDataSet) then
- if not Supports(Value, IXlsDataSet, IDs) then raise Exception.CreateFmt(ErrComponentIsNotXlsDataSet, [Value.Name]);
- FPagesDataSet := Value;
- end;
-
- procedure TFlexCelReport.SetOnRecordCount(const Value: TRecordCountEvent);
- begin
- FOnRecordCount := Value;
- end;
-
- procedure TFlexCelReport.SetDataModule(const Value: TComponent);
- begin
- if Value=nil then FDataModule := Owner else FDataModule:=Value;
- end;
-
- function TFlexCelReport.GetDataSet(const DataSetName: string): IXlsDataSet;
- var
- Ds: TComponent;
- begin
- Ds:=FDataModule.FindComponent(DataSetName);
- if (Ds=nil) then Result:=nil else
- if not Supports(Ds,IXlsDataSet, Result) then
- if (Ds is TDataSet) then Result:=TXlsTDataSet.Create(Ds as TDataSet, RecordCount)
- else Result:=nil;
- end;
-
- function TFlexCelReport.GetPropertyInfo(const PropName: string): PPropInfo;
- begin
- Result:=GetPropInfo(FDataModule, PropName);
- end;
-
- function TFlexCelReport.GetVariantProperty(
- const PropInfo: PPropInfo): variant;
- begin
- Result:=GetVariantProp(FDataModule, PropInfo);
- end;
-
- function TFlexCelReport.GetPagesDataSet: IXlsDataSet;
- var
- IDs:IXlsDataSet;
- begin
- if FPagesDataSet=nil then Result:=nil else
- if (FPagesDataSet is TDataSet) then Result:= TXlsTDataSet.Create(FPagesDataSet as TDataSet, RecordCount)
- else if Supports(FPagesDataSet, IXlsDataSet, IDs) then Result:=IDs
- else Result:=nil;
- end;
-
- function TFlexCelReport.DesignDataModule: TComponent;
- begin
- Result:=FDataModule;
- end;
-
- end.
-