home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2002 October
/
Chip_2002-10_cd1.bin
/
zkuste
/
delphi
/
kolekce
/
d56
/
FLEXCEL.ZIP
/
Demo
/
UTestReport.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
2002-06-15
|
11KB
|
366 lines
unit UTestReport;
interface
{* $DEFINE EXCEL97}
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, DBTables, ExcelReport, DBCtrls, ExtCtrls, Grids, ShellApi,
{$IFDEF Excel97}
Excel97,
{$ELSE}
Excel2000,
{$ENDIF}
DBGrids, UFlexcel, UExcelAdapter, OLEAdapter, XLSAdapter, TemplateStore, JPEG,
UFlexCelImport;
type
TFTestReport = class(TForm)
BtnReport: TButton;
Items: TTable;
ItemsItemNo: TFloatField;
ItemsOrderNo: TFloatField;
ItemsPartNo: TFloatField;
ItemsDescription: TStringField;
ItemsSellPrice: TCurrencyField;
ItemsQty: TIntegerField;
ItemsDiscount: TFloatField;
ItemsExtPrice: TCurrencyField;
Database: TDatabase;
OrdersSource: TDataSource;
ExcelReport2: TExcelReport;
Cust: TTable;
CustCustNo: TFloatField;
CustCompany: TStringField;
CustPhone: TStringField;
CustLastInvoiceDate: TDateTimeField;
CustAddr1: TStringField;
CustAddr2: TStringField;
CustCity: TStringField;
CustState: TStringField;
CustZip: TStringField;
CustCountry: TStringField;
CustFAX: TStringField;
CustTaxRate: TFloatField;
CustContact: TStringField;
Parts: TTable;
PartsPartNo: TFloatField;
PartsDescription: TStringField;
PartsVendorNo: TFloatField;
PartsOnHand: TFloatField;
PartsOnOrder: TFloatField;
PartsBackOrd: TBooleanField;
PartsCost: TCurrencyField;
PartsListPrice: TCurrencyField;
Emps: TTable;
EmpsEmpNo: TIntegerField;
EmpsFullName: TStringField;
EmpsLastName: TStringField;
EmpsFirstName: TStringField;
EmpsPhoneExt: TStringField;
EmpsHireDate: TDateTimeField;
EmpsSalary: TFloatField;
CustSource: TDataSource;
CustTotAddr1: TStringField;
CustTotAddr2: TStringField;
ReportKind: TRadioGroup;
Label1: TLabel;
edCustomer: TDBLookupComboBox;
ExcelReport3: TExcelReport;
ExcelReport4: TExcelReport;
GridOrders: TDBGrid;
Orders: TTable;
OrdersOrderNo: TFloatField;
OrdersCustNo: TFloatField;
OrdersSaleDate: TDateTimeField;
OrdersShipDate: TDateTimeField;
OrdersItemsTotal: TCurrencyField;
OrdersTaxRate: TFloatField;
OrdersFreight: TCurrencyField;
OrdersAmountPaid: TCurrencyField;
OrdersAmountDue: TCurrencyField;
OrdersEmpNo: TIntegerField;
OrdersShipToContact: TStringField;
OrdersShipToAddr1: TStringField;
OrdersShipToAddr2: TStringField;
OrdersShipToCity: TStringField;
OrdersShipToState: TStringField;
OrdersShipToZip: TStringField;
OrdersShipToCountry: TStringField;
OrdersShipToPhone: TStringField;
OrdersShipVIA: TStringField;
OrdersPO: TStringField;
OrdersTerms: TStringField;
OrdersPaymentMethod: TStringField;
OrdersSalesPerson: TStringField;
ExcelReport5: TExcelReport;
Total: TQuery;
TotalSaleDate: TDateTimeField;
TotalCustNo: TFloatField;
TotalCompany: TStringField;
TotalAddr1: TStringField;
TotalAddr2: TStringField;
TotalCity: TStringField;
TotalState: TStringField;
TotalZip: TStringField;
TotalCountry: TStringField;
TotalPhone: TStringField;
TotalFAX: TStringField;
TotalTaxRate: TFloatField;
TotalContact: TStringField;
TotalLastInvoiceDate: TDateTimeField;
TotalOrderNo: TFloatField;
TotalCustNo_1: TFloatField;
TotalShipDate: TDateTimeField;
TotalEmpNo: TIntegerField;
TotalShipToContact: TStringField;
TotalShipToAddr1: TStringField;
TotalShipToAddr2: TStringField;
TotalShipToCity: TStringField;
TotalShipToState: TStringField;
TotalShipToZip: TStringField;
TotalShipToCountry: TStringField;
TotalShipToPhone: TStringField;
TotalShipVIA: TStringField;
TotalPO: TStringField;
TotalTerms: TStringField;
TotalPaymentMethod: TStringField;
TotalItemsTotal: TCurrencyField;
TotalTaxRate_1: TFloatField;
TotalFreight: TCurrencyField;
TotalAmountPaid: TCurrencyField;
TotalOrderNo_1: TFloatField;
TotalItemNo: TFloatField;
TotalPartNo: TFloatField;
TotalQty: TIntegerField;
TotalDiscount: TFloatField;
TotalPartNo_1: TFloatField;
TotalVendorNo: TFloatField;
TotalDescription: TStringField;
TotalOnHand: TFloatField;
TotalOnOrder: TFloatField;
TotalCost: TCurrencyField;
TotalListPrice: TCurrencyField;
GridItems: TDBGrid;
ItemsSource: TDataSource;
ItemsDiscountPc: TFloatField;
ExcelReport1: TExcelReport;
chProtectTheWorkbook: TCheckBox;
chWeb: TCheckBox;
OLEAdapter: TOLEAdapter;
Fish: TTable;
FishSpeciesNo: TFloatField;
FishCommon_Name: TStringField;
FishCategory: TStringField;
FishSpeciesName: TStringField;
FishLengthcm: TFloatField;
FishLength_In: TFloatField;
FishNotes: TMemoField;
FishGraphic: TGraphicField;
XLSAdapter: TXLSAdapter;
XlsTemplateStore1: TXlsTemplateStore;
FlexCelImport1: TFlexCelImport;
Button1: TButton;
procedure BtnReportClick(Sender: TObject);
procedure CustCalcFields(DataSet: TDataSet);
procedure ReportKindClick(Sender: TObject);
procedure ItemsCalcFields(DataSet: TDataSet);
procedure EmpsCalcFields(DataSet: TDataSet);
procedure FormCreate(Sender: TObject);
procedure ExcelReport1AfterGenerateWorkbook(Sender: TObject;
const ExcelApp: TExcelApplication;
const ExcelWorkbook: TExcelWorkbook; const LCID: Integer);
procedure chWebClick(Sender: TObject);
procedure ExcelReport6GetCellValue(Sender: TObject;
const FieldName: WideString; var FieldValue: Variant);
procedure Button1Click(Sender: TObject);
private
ExePath: string;
function GetCurrent_Date: Variant;
procedure UpdateReport(const ExcelReport: TExcelReport);
{ Private declarations }
public
{ Public declarations }
published
property Current_Date: Variant read GetCurrent_Date;
end;
var
FTestReport: TFTestReport;
implementation
{$R *.DFM}
procedure TFTestReport.BtnReportClick(Sender: TObject);
begin
GridOrders.DataSource:=nil; //We don't use OrderSource.DisableControls because it would disable the master/detail relationship....
// In a real app, we should use different datasets for showing data/ reporting
try
GridItems.DataSource:=nil;
try
case ReportKind.ItemIndex of
0: ExcelReport1.Run;
1: ExcelReport2.Run;
2: ExcelReport3.Run;
3: begin
Cust.Filtered:=true; //So we dont use many records
try
ExcelReport4.Run;
finally
Cust.Filtered:=false;
end; //finally
end;
4: ExcelReport6.Run;
end; //case
finally
GridItems.DataSource:=ItemsSource;
end; //finally
finally
GridOrders.DataSource:=OrdersSource;
end; //finally
{$IFNDEF Excel97}
if chWeb.Checked then ShellExecute( Handle,'open', PCHAR(ExePath+'Demo.htm'), NIL,NIL, SW_SHOW);
{$ENDIF}
end;
function TFTestReport.GetCurrent_Date: Variant;
begin
GetCurrent_Date:= DateTimeToStr(Now);
end;
procedure TFTestReport.CustCalcFields(DataSet: TDataSet);
begin
CustTotAddr1.Value:= CustAddr1.Value+' '+ CustAddr2.Value;
CustTotAddr2.Value:= CustCity.Value+' '+ CustState.Value + ' '+ CustZip.Value;
end;
procedure TFTestReport.ReportKindClick(Sender: TObject);
begin
edCustomer.Enabled:= ReportKind.ItemIndex=1;
end;
procedure TFTestReport.ItemsCalcFields(DataSet: TDataSet);
begin
ItemsExtPrice.Value := ItemsQty.Value *
ItemsSellPrice.Value * (100 - ItemsDiscount.Value) / 100;
ItemsDiscountPc.Value:=ItemsDiscount.Value / 100;
end;
procedure TFTestReport.EmpsCalcFields(DataSet: TDataSet);
begin
EmpsFullName.Value := Format('%s, %s', [EmpsLastName.Value, EmpsFirstName.Value]);
end;
procedure TFTestReport.FormCreate(Sender: TObject);
begin
ExePath := ExtractFilePath(Paramstr(0));
ExcelReport1.Template:=ExePath+'Templates\SimpleDemo.xls';
ExcelReport2.Template:=ExePath+'Templates\Invoices.xls';
ExcelReport3.Template:=ExePath+'Templates\InvoicesAll.xls';
ExcelReport4.Template:=ExePath+'Templates\Invoices.xls';
if ExcelReport6.Adapter=OleAdapter then ExcelReport6.Template:=ExePath+'Templates\FishFacts.xls'
else ExcelReport6.Template:='FishFacts.xls';
{$IFDEF Excel97}
chWeb.Enabled:=false;
{$ENDIF}
end;
procedure TFTestReport.ExcelReport1AfterGenerateWorkbook(Sender: TObject;
const ExcelApp: TExcelApplication; const ExcelWorkbook: TExcelWorkbook;
const LCID: Integer);
begin
try
if chProtectTheWorkbook.Checked then ExcelWorkbook.Protect;
ExcelWorkbook.PrintOut;
except
on e: Exception do Application.ShowException(e);
//Clear the exception so the report gets generated anyway
end; //except
end;
procedure TFTestReport.UpdateReport(const ExcelReport: TExcelReport);
begin
{$IFNDEF Excel97}
if chWeb.Checked then
begin
ExcelReport.AutoClose:=true;
ExcelReport.FileName:=ExePath+'Demo';
ExcelReport.SaveFormatBasic:=[saHtml];
end
else ExcelReport.AutoClose:=false;
{$ENDIF}
end;
procedure TFTestReport.chWebClick(Sender: TObject);
begin
UpdateReport(ExcelReport1);
UpdateReport(ExcelReport2);
UpdateReport(ExcelReport3);
UpdateReport(ExcelReport4);
UpdateReport(ExcelReport5);
end;
procedure TFTestReport.ExcelReport6GetCellValue(Sender: TObject;
const FieldName: WideString; var FieldValue: Variant);
{ Paradox graphic BLOB header }
type
TGraphicHeader = record
Count: Word; { Fixed at 1 }
HType: Word; { Fixed at $0100 }
Size: Longint; { Size not including header }
end;
var
Jp: TJPEGImage;
Bmp: Graphics.TBitmap;
Ms: TMemoryStream;
s:string;
begin
if (FieldName='##FISH##NOTES') and (random(3)=1) then FieldValue:='';
if FieldName='##FISH##Graphic##JPEG' then
begin
Jp:= TJPEGImage.Create;
try
Bmp:= TBitmap.Create;
try
Ms:= TMemoryStream.Create;
try
s:=FieldValue;
Ms.Write(s[1+SizeOf(TGraphicHeader)], Length(s)-SizeOf(TGraphicHeader));
Ms.Position:=0;
Bmp.LoadFromStream(Ms);
Jp.Assign(Bmp);
Ms.Clear;
Jp.SaveToStream(Ms);
Ms.Position:=0;
setLength(s, Ms.Size);
Ms.Read(s[1],Ms.Size);
FieldValue:=s;
finally
FreeAndNil(Ms);
end; //finally
finally
FreeAndNil(Bmp);
end; //finally
finally
FreeAndNil(Jp);
end; //finally
end;
end;
procedure TFTestReport.Button1Click(Sender: TObject);
begin
FlexCelImport1.OpenFile('c:\adrian\excel\libro1.xls');
FlexCelImport1.CellValue[1,2]:='Aaay';
showmessage(FlexCelImport1.CellValue[1,2]);
FlexCelImport1.Save('c:\adrian\excel\libro1pp.xls');
FlexCelImport1.CloseFile;
end;
end.