home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd1.bin / zkuste / delphi / kolekce / d567 / FLEXCEL.ZIP / FlexCel / UFlexCelReport.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2002-10-11  |  6.3 KB  |  209 lines

  1. unit UFlexCelReport;
  2. {******************************************************************
  3.  Component to automate reporting to Excel
  4.  Version 2.5
  5.  
  6. // The contents of this file are subject to the Mozilla Public License
  7. // Version 1.1 (the "License"); you may not use this file except in compliance
  8. // with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
  9. //
  10. // Software distributed under the License is distributed on an "AS IS" basis,
  11. // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
  12. // specific language governing rights and limitations under the License.
  13. //
  14. // The original code is Excel.pas, released January 23, 2002.
  15. //
  16. // The initial developer of the original code is Adrian Gallero,
  17. // written by Adrian Gallero (agallero@netscape.net).
  18. //
  19. // Portions created by Adrian Gallero are Copyright
  20. // (C) 2002-2002 Adrian Gallero. All Rights Reserved.
  21.  
  22.  Send any comments to agallero@netscape.net
  23. ******************************************************************** }
  24. interface
  25. {$R IFlexcel.res}
  26. uses
  27.   SysUtils, Classes, UCustomFlexCelReport, UXlsDB, UXlsTDataSet,
  28.   contnrs, DB,
  29.   {$IFDEF ConditionalExpressions}{$if CompilerVersion >= 14} variants,{$IFEND}{$ENDIF} //Delphi 6 or above
  30.   typinfo, UExcelAdapter, UFlxMessages, UFlxFullDataSets;
  31.  
  32. type
  33.   TCalcRecordCount=(cr_None, cr_Count, cr_SlowCount);
  34.   TRecordCountEvent = procedure (Sender: TObject; const DataSet: TDataSet; var RecordCount: integer) of object;
  35.  
  36.   TFlexCelReport = class(TCustomFlexCelReport)
  37.   private
  38.     FDataModule: TComponent;
  39.     FCalcRecordCount: TCalcRecordCount;
  40.     FPagesDataSet: TFlxDataSet; 
  41.  
  42.     FOnRecordCount: TRecordCountEvent;
  43.  
  44.     procedure SetPagesDataSet(const Value: TFlxDataSet);
  45.     procedure SetOnRecordCount(const Value: TRecordCountEvent);
  46.     procedure SetDataModule(const Value: TComponent);
  47.  
  48.     function RecordCount(const DbSet: TDataSet): integer;
  49.     { Private declarations }
  50.  
  51.   protected
  52.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  53.     function GetDataSet(const DataSetName: string): IXlsDataSet; override;
  54.     function GetPropertyInfo(const PropName: string): PPropInfo; override;
  55.     function GetVariantProperty(const PropInfo: PPropInfo): variant; override;
  56.     function GetPagesDataSet: IXlsDataSet; override;
  57.  
  58.     { Protected declarations }
  59.  
  60.   public
  61.     constructor Create(AOwner:TComponent);override;
  62.     destructor Destroy;override;
  63.  
  64.     function DesignDataModule: TComponent; override;
  65.     { Public declarations }
  66.  
  67.   published
  68.     property DataModule: TComponent read FDataModule write SetDataModule;
  69.     property CalcRecordCount: TCalcRecordCount read FCalcRecordCount write FCalcRecordCount default cr_Count;
  70.     property PagesDataSet: TFlxDataSet read FPagesDataSet write SetPagesDataSet;
  71.  
  72.     //Events
  73.     property OnRecordCount: TRecordCountEvent read FOnRecordCount write SetOnRecordCount;
  74.     { Published declarations }
  75.  
  76.   end;
  77.  
  78.   ClassOfTFlexCelReport= class of TFlexCelReport;
  79. procedure Register;
  80.  
  81. implementation
  82.  
  83. procedure Register;
  84. begin
  85.   RegisterComponents('FlexCel', [TFlexcelReport]);
  86. end;
  87.  
  88.  
  89. { TFlexCelReport }
  90.  
  91. constructor TFlexCelReport.Create(AOwner: TComponent);
  92. begin
  93.   inherited;
  94.   FDataModule:=AOwner;
  95.   FCalcRecordCount:=cr_Count;
  96. end;
  97.  
  98. destructor TFlexCelReport.Destroy;
  99. begin
  100.   inherited;
  101. end;
  102.  
  103. procedure TFlexCelReport.Notification(AComponent: TComponent; Operation: TOperation);
  104. begin
  105.   inherited Notification(AComponent, Operation);
  106.   if Operation = opRemove then
  107.   begin
  108.     if AComponent = FPagesDataSet then
  109.         FPagesDataSet:= nil;
  110.     if AComponent = FDataModule then
  111.         FDataModule:= Owner;
  112.   end;
  113. end;
  114.  
  115. ////////////////////////////////////////////////////////////////////////////////
  116. function TFlexCelReport.RecordCount(const DbSet: TDataSet): integer;
  117. begin
  118.   Result:=0;
  119.   if Canceled then exit;
  120.   if DbSet<>nil then
  121.   begin
  122.     //Count the records
  123.     DbSet.First;
  124.  
  125.     //If the event OnCountRecords is Assigned, the var FCalcRecordCount has no meaning
  126.     if Assigned (FOnRecordCount) then FOnRecordCount(Self, DbSet, Result)
  127.     else
  128.       case FCalcRecordCount of
  129.         cr_None:
  130.           Result:= DbSet.RecordCount;
  131.         cr_Count:
  132.         begin
  133.           DbSet.Last;
  134.           DbSet.First;
  135.           Result:= DbSet.RecordCount;
  136.         end; //cr_Count
  137.         cr_SlowCount:
  138.         begin
  139.           while not DbSet.Eof do
  140.           begin
  141.             inc(Result);
  142.             DbSet.Next;
  143.           end;
  144.           DbSet.First;
  145.         end; //cr_SlowCount
  146.       end; //case
  147.   end //DbSet <>nil
  148.   else Result:=1;
  149. end;
  150.  
  151. procedure TFlexCelReport.SetPagesDataSet(const Value: TFlxDataSet);
  152. var
  153.   IDs: IXlsDataSet;
  154. begin
  155.   if (Value<>nil) then
  156.     if not (Value is TDataSet) then
  157.      if not Supports(Value, IXlsDataSet, IDs) then raise Exception.CreateFmt(ErrComponentIsNotXlsDataSet, [Value.Name]);
  158.   FPagesDataSet := Value;
  159. end;
  160.  
  161. procedure TFlexCelReport.SetOnRecordCount(const Value: TRecordCountEvent);
  162. begin
  163.   FOnRecordCount := Value;
  164. end;
  165.  
  166. procedure TFlexCelReport.SetDataModule(const Value: TComponent);
  167. begin
  168.   if Value=nil then FDataModule := Owner else FDataModule:=Value;
  169. end;
  170.  
  171. function TFlexCelReport.GetDataSet(const DataSetName: string): IXlsDataSet;
  172. var
  173.   Ds: TComponent;
  174. begin
  175.   Ds:=FDataModule.FindComponent(DataSetName);
  176.   if (Ds=nil) then Result:=nil else
  177.   if not Supports(Ds,IXlsDataSet, Result) then
  178.   if (Ds is TDataSet) then Result:=TXlsTDataSet.Create(Ds as TDataSet, RecordCount)
  179.   else Result:=nil;
  180. end;
  181.  
  182. function TFlexCelReport.GetPropertyInfo(const PropName: string): PPropInfo;
  183. begin
  184.   Result:=GetPropInfo(FDataModule, PropName);
  185. end;
  186.  
  187. function TFlexCelReport.GetVariantProperty(
  188.   const PropInfo: PPropInfo): variant;
  189. begin
  190.   Result:=GetVariantProp(FDataModule, PropInfo);
  191. end;
  192.  
  193. function TFlexCelReport.GetPagesDataSet: IXlsDataSet;
  194. var
  195.   IDs:IXlsDataSet;
  196. begin
  197.   if FPagesDataSet=nil then Result:=nil else
  198.     if (FPagesDataSet is TDataSet) then Result:= TXlsTDataSet.Create(FPagesDataSet as TDataSet, RecordCount)
  199.     else if Supports(FPagesDataSet, IXlsDataSet, IDs) then Result:=IDs
  200.     else Result:=nil;
  201. end;
  202.  
  203. function TFlexCelReport.DesignDataModule: TComponent;
  204. begin
  205.   Result:=FDataModule;
  206. end;
  207.  
  208. end.
  209.