home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 October / Chip_2002-10_cd1.bin / zkuste / delphi / kolekce / d56 / FLEXCEL.ZIP / Demo / UFlDemoData.pas < prev    next >
Pascal/Delphi Source File  |  2002-07-03  |  15KB  |  494 lines

  1. unit UFlDemoData;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   UFlexcelReport, TemplateStore, UFlexCelImport, OLEAdapter, UExcelAdapter,
  8.   XLSAdapter, ImgList, Db, DBTables, Grids, 
  9.   {$IFDEF Excel97}Excel97,{$ELSE}Excel2000,{$ENDIF}
  10.   {$IFDEF ConditionalExpressions}{$if CompilerVersion >= 14} variants,{$IFEND}{$ENDIF} //Delphi 6 or above
  11.  
  12.   JPEG, ExtDlgs;
  13.  
  14. type
  15.   TDemoData = class(TDataModule)
  16.     ImageList1: TImageList;
  17.     ImageList2: TImageList;
  18.     ImageList3: TImageList;
  19.     XlsSaveDialog: TSaveDialog;
  20.     XLSAdapter: TXLSAdapter;
  21.     OLEAdapter: TOLEAdapter;
  22.     FlexCelImport: TFlexCelImport;
  23.     XlsTemplateStore: TXlsTemplateStore;
  24.     RepSimpleDemo: TFlexCelReport;
  25.     Total: TQuery;
  26.     TotalSaleDate: TDateTimeField;
  27.     TotalCustNo: TFloatField;
  28.     TotalCompany: TStringField;
  29.     TotalAddr1: TStringField;
  30.     TotalAddr2: TStringField;
  31.     TotalCity: TStringField;
  32.     TotalState: TStringField;
  33.     TotalZip: TStringField;
  34.     TotalCountry: TStringField;
  35.     TotalPhone: TStringField;
  36.     TotalFAX: TStringField;
  37.     TotalTaxRate: TFloatField;
  38.     TotalContact: TStringField;
  39.     TotalLastInvoiceDate: TDateTimeField;
  40.     TotalOrderNo: TFloatField;
  41.     TotalCustNo_1: TFloatField;
  42.     TotalShipDate: TDateTimeField;
  43.     TotalEmpNo: TIntegerField;
  44.     TotalShipToContact: TStringField;
  45.     TotalShipToAddr1: TStringField;
  46.     TotalShipToAddr2: TStringField;
  47.     TotalShipToCity: TStringField;
  48.     TotalShipToState: TStringField;
  49.     TotalShipToZip: TStringField;
  50.     TotalShipToCountry: TStringField;
  51.     TotalShipToPhone: TStringField;
  52.     TotalShipVIA: TStringField;
  53.     TotalPO: TStringField;
  54.     TotalTerms: TStringField;
  55.     TotalPaymentMethod: TStringField;
  56.     TotalItemsTotal: TCurrencyField;
  57.     TotalTaxRate_1: TFloatField;
  58.     TotalFreight: TCurrencyField;
  59.     TotalAmountPaid: TCurrencyField;
  60.     TotalOrderNo_1: TFloatField;
  61.     TotalItemNo: TFloatField;
  62.     TotalPartNo: TFloatField;
  63.     TotalQty: TIntegerField;
  64.     TotalDiscount: TFloatField;
  65.     TotalPartNo_1: TFloatField;
  66.     TotalVendorNo: TFloatField;
  67.     TotalDescription: TStringField;
  68.     TotalOnHand: TFloatField;
  69.     TotalOnOrder: TFloatField;
  70.     TotalCost: TCurrencyField;
  71.     TotalListPrice: TCurrencyField;
  72.     Orders: TTable;
  73.     OrdersOrderNo: TFloatField;
  74.     OrdersCustNo: TFloatField;
  75.     OrdersSaleDate: TDateTimeField;
  76.     OrdersShipDate: TDateTimeField;
  77.     OrdersItemsTotal: TCurrencyField;
  78.     OrdersTaxRate: TFloatField;
  79.     OrdersFreight: TCurrencyField;
  80.     OrdersAmountPaid: TCurrencyField;
  81.     OrdersAmountDue: TCurrencyField;
  82.     OrdersEmpNo: TIntegerField;
  83.     OrdersShipToContact: TStringField;
  84.     OrdersShipToAddr1: TStringField;
  85.     OrdersShipToAddr2: TStringField;
  86.     OrdersShipToCity: TStringField;
  87.     OrdersShipToState: TStringField;
  88.     OrdersShipToZip: TStringField;
  89.     OrdersShipToCountry: TStringField;
  90.     OrdersShipToPhone: TStringField;
  91.     OrdersSalesPerson: TStringField;
  92.     OrdersShipVIA: TStringField;
  93.     OrdersPO: TStringField;
  94.     OrdersTerms: TStringField;
  95.     OrdersPaymentMethod: TStringField;
  96.     Items: TTable;
  97.     ItemsItemNo: TFloatField;
  98.     ItemsOrderNo: TFloatField;
  99.     ItemsPartNo: TFloatField;
  100.     ItemsDescription: TStringField;
  101.     ItemsSellPrice: TCurrencyField;
  102.     ItemsQty: TIntegerField;
  103.     ItemsDiscount: TFloatField;
  104.     ItemsExtPrice: TCurrencyField;
  105.     ItemsDiscountPc: TFloatField;
  106.     Parts: TTable;
  107.     PartsPartNo: TFloatField;
  108.     PartsDescription: TStringField;
  109.     PartsVendorNo: TFloatField;
  110.     PartsOnHand: TFloatField;
  111.     PartsOnOrder: TFloatField;
  112.     PartsBackOrd: TBooleanField;
  113.     PartsCost: TCurrencyField;
  114.     PartsListPrice: TCurrencyField;
  115.     Emps: TTable;
  116.     EmpsEmpNo: TIntegerField;
  117.     EmpsFullName: TStringField;
  118.     EmpsLastName: TStringField;
  119.     EmpsFirstName: TStringField;
  120.     EmpsPhoneExt: TStringField;
  121.     EmpsHireDate: TDateTimeField;
  122.     EmpsSalary: TFloatField;
  123.     Cust: TTable;
  124.     CustCustNo: TFloatField;
  125.     CustCompany: TStringField;
  126.     CustPhone: TStringField;
  127.     CustLastInvoiceDate: TDateTimeField;
  128.     CustAddr1: TStringField;
  129.     CustAddr2: TStringField;
  130.     CustCity: TStringField;
  131.     CustState: TStringField;
  132.     CustZip: TStringField;
  133.     CustCountry: TStringField;
  134.     CustFAX: TStringField;
  135.     CustTaxRate: TFloatField;
  136.     CustContact: TStringField;
  137.     CustTotAddr1: TStringField;
  138.     CustTotAddr2: TStringField;
  139.     Fish: TTable;
  140.     FishSpeciesNo: TFloatField;
  141.     FishCommon_Name: TStringField;
  142.     FishCategory: TStringField;
  143.     FishSpeciesName: TStringField;
  144.     FishLengthcm: TFloatField;
  145.     FishLength_In: TFloatField;
  146.     FishNotes: TMemoField;
  147.     FishGraphic: TGraphicField;
  148.     HTMLSaveDialog: TSaveDialog;
  149.     RepVarArray: TFlexCelReport;
  150.     DsCust: TDataSource;
  151.     RepDbDemo: TFlexCelReport;
  152.     DsOrders: TDataSource;
  153.     RepMultMast: TFlexCelReport;
  154.     RepSheets: TFlexCelReport;
  155.     RepFishFacts: TFlexCelReport;
  156.     RepPivot: TFlexCelReport;
  157.     RepEvents: TFlexCelReport;
  158.     Events: TTable;
  159.     EventsEventNo: TAutoIncField;
  160.     EventsVenueNo: TIntegerField;
  161.     EventsEvent_Name: TStringField;
  162.     EventsEvent_Date: TDateField;
  163.     EventsEvent_Time: TTimeField;
  164.     EventsEvent_Description: TMemoField;
  165.     EventsTicket_price: TCurrencyField;
  166.     EventsEvent_Photo: TGraphicField;
  167.     RepCustom: TFlexCelReport;
  168.     Ds: TQuery;
  169.     RepCharts: TFlexCelReport;
  170.     SortedItems: TQuery;
  171.     OpenDialog: TOpenDialog;
  172.     OpenPictureDialog: TOpenPictureDialog;
  173.     RepImgDemo: TFlexCelReport;
  174.     procedure ItemsCalcFields(DataSet: TDataSet);
  175.     procedure EmpsCalcFields(DataSet: TDataSet);
  176.     procedure CustCalcFields(DataSet: TDataSet);
  177.     procedure RepFishFactsGetCellValue(Sender: TObject;
  178.       const FieldName: WideString; var FieldValue: Variant);
  179.     procedure RepEventsGetCellValue(Sender: TObject;
  180.       const FieldName: WideString; var FieldValue: Variant);
  181.     procedure RepAfterGenerateWorkbook(Sender: TObject;
  182.       const ExcelApp: TExcelFile);
  183.   private
  184.     FVarArrayDemo: variant;
  185.     FProtect: boolean;
  186.     function GetCurrent_Date: variant;
  187.     function GetAvailability: variant;
  188.     function GetCurrentDate: variant;
  189.     function GetCurrentSQL: variant;
  190.     function GetDsName: variant;
  191.     function GetMyImage: variant;
  192.     { Private declarations }
  193.   public
  194.     MyImageFilename: string;
  195.  
  196.     procedure SetAdapter (const Adapter: TExcelAdapter);
  197.     procedure SetFileName (const FName: string);
  198.  
  199.     procedure FillArrayDemo(const aStg: array of TStringGrid);
  200.     function GetReport(const Tag: integer): TFlexCelReport;
  201.  
  202.     property Protect: boolean read FProtect write FProtect;
  203.     { Public declarations }
  204.   published
  205.     property Current_Date: variant read GetCurrent_Date;
  206.     property VarArrayDemo: variant read FVarArrayDemo;
  207.     property Availability: variant read GetAvailability;
  208.  
  209.     property DsName: variant read GetDsName;
  210.     property CurrentDate: variant read GetCurrentDate;
  211.     property CurrentSQL: variant read GetCurrentSQL;
  212.  
  213.     property MyImage: variant read GetMyImage;
  214.  
  215.   end;
  216.  
  217. var
  218.   DemoData: TDemoData;
  219.  
  220.  
  221. implementation
  222.  
  223. {$R *.DFM}
  224.  
  225. { TDemoData }
  226.  
  227. procedure TDemoData.SetAdapter(const Adapter: TExcelAdapter);
  228. var
  229.   i: integer;
  230. begin
  231.   for i:=0 to ComponentCount-1 do
  232.   begin
  233.     if (Components[i] is TFlexCelReport) then (Components[i] as TFlexCelReport).Adapter:= Adapter;
  234.     if (Components[i] is TFlexCelImport) then (Components[i] as TFlexCelImport).Adapter:= Adapter;
  235.   end;
  236. end;
  237.  
  238. function TDemoData.GetCurrent_Date: variant;
  239. begin
  240.   Result:=now;
  241. end;
  242.  
  243. procedure TDemoData.SetFileName(const FName: string);
  244. var
  245.   i: integer;
  246. begin
  247.   for i:=0 to ComponentCount-1 do
  248.   begin
  249.     if (Components[i] is TFlexCelReport) then (Components[i] as TFlexCelReport).FileName:= FName;
  250.   end;
  251. end;
  252.  
  253.  
  254.  
  255. procedure TDemoData.FillArrayDemo(const aStg: array of TStringGrid);
  256. var
  257.   i,j, k: integer;
  258.   e: extended;
  259. begin
  260.   FVarArrayDemo:=VarArrayCreate([Low(aStg), High(aStg), 0,aStg[Low(aStg)].RowCount-2,0,aStg[Low(aStg)].ColCount-2], VarVariant);
  261.   for k:= Low(aStg) to High(aStg) do
  262.     for i:=0 to aStg[k].RowCount-2 do
  263.       for j:=0 to aStg[k].ColCount-2 do
  264.         //try to convert to number
  265.         if TextToFloat(PChar(aStg[k].Cells[j+1,i+1]), e, fvExtended) then  //Dont use val because it doesnt handle locales
  266.           FVarArrayDemo[k,i,j]:=e else
  267.           FVarArrayDemo[k,i,j]:=aStg[k].Cells[j+1,i+1];
  268.  
  269. end;
  270.  
  271. function TDemoData.GetReport(const Tag: integer): TFlexCelReport;
  272. begin
  273.   case Tag of
  274.     1:  Result:= RepSimpleDemo;
  275.     2:  Result:= RepVarArray;
  276.     3:  Result:= RepImgDemo;
  277.     4:  Result:= RepDbDemo;
  278.     5:  Result:= RepMultMast;
  279.     6:  Result:= RepSheets;
  280.     7:  Result:= RepPivot;
  281.     8:  Result:= RepCharts;
  282.     9:  Result:= RepFishFacts;
  283.    10:  Result:= RepEvents;
  284.    11: Result:= RepCustom;
  285.     else Raise Exception.Create('Invalid tag');
  286.   end; //Case
  287. end;
  288.  
  289. procedure TDemoData.ItemsCalcFields(DataSet: TDataSet);
  290. begin
  291.   ItemsExtPrice.Value := ItemsQty.Value *
  292.     ItemsSellPrice.Value * (100 - ItemsDiscount.Value) / 100;
  293.   ItemsDiscountPc.Value:=ItemsDiscount.Value / 100;
  294. end;
  295.  
  296. procedure TDemoData.EmpsCalcFields(DataSet: TDataSet);
  297. begin
  298.   EmpsFullName.Value := Format('%s, %s', [EmpsLastName.Value, EmpsFirstName.Value]);
  299. end;
  300.  
  301. procedure TDemoData.CustCalcFields(DataSet: TDataSet);
  302. begin
  303.   CustTotAddr1.Value:= CustAddr1.Value+' '+ CustAddr2.Value;
  304.   CustTotAddr2.Value:= CustCity.Value+' '+ CustState.Value + ' '+ CustZip.Value;
  305. end;
  306.  
  307.  
  308. { Paradox graphic BLOB header }
  309. type
  310.   TGraphicHeader = record
  311.     Count: Word;                { Fixed at 1 }
  312.     HType: Word;                { Fixed at $0100 }
  313.     Size: Longint;              { Size not including header }
  314.   end;
  315.  
  316. procedure TDemoData.RepFishFactsGetCellValue(Sender: TObject;
  317.   const FieldName: WideString; var FieldValue: Variant);
  318.  
  319. var
  320.   Jp: TJPEGImage;
  321.   Bmp: Graphics.TBitmap;
  322.   Ms: TMemoryStream;
  323.   s:string;
  324. begin
  325.   if FieldName='##FISH##Graphic##JPEG' then
  326.   begin
  327.     Jp:= TJPEGImage.Create;
  328.     try
  329.       Bmp:= TBitmap.Create;
  330.       try
  331.         Ms:= TMemoryStream.Create;
  332.         try
  333.           s:=FieldValue;
  334.           Ms.Write(s[1+SizeOf(TGraphicHeader)], Length(s)-SizeOf(TGraphicHeader));
  335.           Ms.Position:=0;
  336.           Bmp.LoadFromStream(Ms);
  337.           Jp.Assign(Bmp);
  338.           Ms.Clear;
  339.           Jp.SaveToStream(Ms);
  340.           Ms.Position:=0;
  341.           setLength(s, Ms.Size);
  342.           Ms.Read(s[1],Ms.Size);
  343.           FieldValue:=s;
  344.         finally
  345.           FreeAndNil(Ms);
  346.         end; //finally
  347.       finally
  348.         FreeAndNil(Bmp);
  349.       end; //finally
  350.     finally
  351.       FreeAndNil(Jp);
  352.     end; //finally
  353.   end;
  354. end;
  355.  
  356. procedure TDemoData.RepEventsGetCellValue(Sender: TObject;
  357.   const FieldName: WideString; var FieldValue: Variant);
  358. var
  359.   Jp: TJPEGImage;
  360.   Bmp: Graphics.TBitmap;
  361.   Ms: TMemoryStream;
  362.   s:string;
  363. begin
  364.   if FieldName='##Events##Event_Photo##JPEG' then
  365.   begin
  366.     Jp:= TJPEGImage.Create;
  367.     try
  368.       Bmp:= TBitmap.Create;
  369.       try
  370.         Ms:= TMemoryStream.Create;
  371.         try
  372.           s:=FieldValue;
  373.           Ms.Write(s[1+SizeOf(TGraphicHeader)], Length(s)-SizeOf(TGraphicHeader));
  374.           Ms.Position:=0;
  375.           Bmp.LoadFromStream(Ms);
  376.           Jp.Assign(Bmp);
  377.           Ms.Clear;
  378.           Jp.SaveToStream(Ms);
  379.           Ms.Position:=0;
  380.           setLength(s, Ms.Size);
  381.           Ms.Read(s[1],Ms.Size);
  382.           FieldValue:=s;
  383.         finally
  384.           FreeAndNil(Ms);
  385.         end; //finally
  386.       finally
  387.         FreeAndNil(Bmp);
  388.       end; //finally
  389.     finally
  390.       FreeAndNil(Jp);
  391.     end; //finally
  392.   end;
  393. end;
  394.  
  395. function TDemoData.GetAvailability: variant;
  396. var
  397.   w:Widestring;
  398.   wc: word;
  399. begin
  400.   if (EventsEventNo.Value=2) or (EventsEventNo.Value=4) then
  401.     begin
  402.       //There should be a better way to create a widestring... but this works
  403.       setLength(w,6);
  404.       wc:=$FB46;move(wc,w[1],2);
  405.       wc:=64335; move(wc,w[2],2);
  406.       wc:=65209; move(wc,w[3],2);
  407.       wc:=65272; move(wc,w[4],2);
  408.       wc:=65153; move(wc,w[5],2);
  409.       wc:=65179; move(wc,w[6],2);
  410.       w:='This is some no-no-sense unicode: '+w;
  411.     end else w:='';
  412.   result:=w;
  413. end;
  414.  
  415. function TDemoData.GetCurrentDate: variant;
  416. begin
  417.   Result:= double(Now); //Dates should be passed as numbers, and the corresponding cell in excel have Date format.
  418. end;
  419.  
  420. function TDemoData.GetCurrentSQL: variant;
  421. begin
  422.   Result:=''; //Not implemented here, see DbDump.exe for an implementation
  423. end;
  424.  
  425. function TDemoData.GetDsName: variant;
  426. begin
  427.   Result:='DBDEMOS';
  428. end;
  429.  
  430. procedure TDemoData.RepAfterGenerateWorkbook(Sender: TObject;
  431.   const ExcelApp: TExcelFile);
  432. var
  433.   Wb: TExcelWorkbook;
  434.   i: integer; v:variant ;
  435. begin
  436.   if Protect and ((Sender as TFlexCelReport).Adapter is TOLEAdapter) then
  437.   begin
  438.     Wb:=(ExcelApp as TOleFile ).ExcelWorkbook;
  439.     Wb.Protect('pass',EmptyParam, EmptyParam);
  440.     for i:=1 to Wb.Sheets.Count do
  441.     begin
  442.       //we could use (Wb.Sheets[i] as WorkSheet).Protect('pass', EmptyParam, EmptyParam, EmptyParam, EmptyParam, (ExcelApp as TOleFile ).LCID);
  443.       //if there were no charts
  444.       v:=Wb.Sheets[i];v.Protect('pass');
  445.     end;
  446.   end;
  447. end;
  448.  
  449. function TDemoData.GetMyImage: variant;
  450. var
  451.   Fs: TFileStream;
  452.   Pic: TPicture;
  453.   Jp: TJPEGImage;
  454.   Ms: TMemoryStream;
  455.   s: string;
  456. begin
  457.   if Uppercase(ExtractFileExt(MyImageFilename))<>'.JPG' then
  458.   begin //We have to convert it to a JPEG
  459.     Pic:=TPicture.Create;
  460.     try
  461.       Jp:= TJPEGImage.Create;
  462.       try
  463.         Pic.LoadFromFile(MyImageFilename);
  464.         Jp.Assign(Pic.Graphic);
  465.         Ms:= TMemoryStream.Create;
  466.         try
  467.           Jp.SaveToStream(Ms);
  468.           Ms.Position:=0;
  469.           setLength(s, Ms.Size);
  470.           Ms.Read(s[1],Ms.Size);
  471.           Result:=s;
  472.         finally
  473.           FreeAndNil(Ms);
  474.         end; //finally
  475.       finally
  476.         FreeAndNil(Jp);
  477.       end; //finally
  478.     finally
  479.       FreeAndNil(Pic);
  480.     end; //finally
  481.   end else //File is already JPEG, we dont need to convert it
  482.   begin
  483.     Fs:= TFileStream.Create(MyImageFilename, fmOpenRead);
  484.     try
  485.       SetLength(s, Fs.Size);
  486.       Fs.Read(s[1], Length(s));
  487.       Result:=s;
  488.     finally
  489.       FreeAndNil(Fs);
  490.     end; //finally
  491.   end;
  492. end;
  493. end.
  494.