用Delphi开发报表打印的ASP组件

2/4/2006来源:Delphi教程人气:11756

     asp在对数据库操作方面提供了很好的内嵌对象。但是,其他方面的功能有所欠缺,如:报表打印。特别是国内的复杂的报表,用ASP很难实现。

       不过,ASP支持插件,这就可以根据需要开发ASP插件,笔者用Delphi5.0开发了ASP打印组件。下面笔者将分步来开发一个通用的报表打印的ASP组件。

       第一步:新建一个Activex Library,命名为PRintT,然后再新建一个Active Server Object Class,命名为Print,即建立了一个名为Print的ASP组件,文件命名为Unit1.pas。

      第二步:打开Type Library,新建一个方法Print1,用于传递报表打印。

      第三步:新建一个DataModule,放入Adoconnection组件和AdoTable组件,文件名为Unit2.pas。

     第四步:新建一个TQuickRep,设计你要打印的报表,文件名为文件名为Unit23.pas。以下是个文件的详细代码:
{=============Unit1.pas===============}

unit Unit1;

interface

uses
  ComObj, ActiveX, AspTlb, PrintT_TLB, StdVcl;

type
  TPrint = class(TASPObject, iprint)
  protected
    procedure OnEndPage; safecall;
    procedure OnStartPage(const AScriptingContext: IUnknown); safecall;
    procedure Print1; safecall;
  end;

implementation

uses ComServ,unit2,unit23;

procedure TPrint.OnEndPage;
begin
  inherited OnEndPage;
end;

procedure TPrint.OnStartPage(const AScriptingContext: IUnknown);
begin
  inherited OnStartPage(AScriptingContext);
end;

procedure TPrint.Print1;
begin
     IdearRpt.Print;
end;

initialization
  TAutoObjectFactory.Create(ComServer, TPrint, Class_Print,
    ciMultiInstance, tmApartment);
end.

{===============Unit2.pas===============}

unit Unit2;

interface

uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms,
  Dialogs, DBTables, DB, ADODB;

type
  TCustomerData = class(TDataModule)
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    ADOTable1NIANH: TStringField;
    ADOTable1XUH: TStringField;
    ADOTable1SHENQDW: TStringField;
    ADOTable1SHENQRQ: TDateTimeField;
    ADOTable1FARDB: TStringField;
    ADOTable1FARDH: TStringField;
    ADOTable1LIANXR: TStringField;
    ADOTable1LIANXRDH: TStringField;
    ADOTable1DANWZCDZ: TStringField;
    ADOTable1JIANSXZDM: TStringField;
    ADOTable1JIANSGM: TBCDField;
    ADOTable1ZHIGRS: TBCDField;
    ADOTable1YONGDFH: TBCDField;
    ADOTable1RIYSL: TBCDField;
    ADOTable1RIPSL: TBCDField;
    ADOTable1JIANZSJGD: TBCDField;
    ADOTable1RANQYL: TBCDField;
    ADOTable1YONGDYQ: TStringField;
    ADOTable1RELYL: TBCDField;
    ADOTable1JIANSXM: TStringField;
    ADOTable1PIZWH: TStringField;
    ADOTable1JIANSGM2: TBCDField;
    ADOTable1YONGDXZDM: TStringField;
    ADOTable1YONGDWZ: TStringField;
    ADOTable1DONGL: TStringField;
    ADOTable1XIL: TStringField;
    ADOTable1NANL: TStringField;
    ADOTable1BEIL: TStringField;
    ADOTable1CHANG: TBCDField;
    ADOTable1KUAN: TBCDField;
    ADOTable1ZONGMJ1: TBCDField;
    ADOTable1ZONGMJ2: TBCDField;
    ADOTable1YOUXRQ1: TDateTimeField;
    ADOTable1YOUXRQ2: TDateTimeField;
    ADOTable1BEIZ: TStringField;
    ADOTable1JINGB: TStringField;
    ADOTable1SHENH: TStringField;
    ADOTable1SHEND: TStringField;
    ADOTable1SHENDRQ: TDateTimeField;
    ADOTable1PICTURE: TBlobField;

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  CustomerData: TCustomerData;

implementation

{$R *.DFM}

 

end.

{===========Unit23.pas===============}

unit Unit23;

interface

uses Windows, SysUtils, Messages, Classes, Graphics, Controls,
  StdCtrls, ExtCtrls, Forms, Quickrpt, QRCtrls;

type
  TIdearRpt = class(TQuickRep)
    PageFooterBand1: TQRBand;
    PageHeaderBand1: TQRBand;
    QRLabel1: TQRLabel;
    QRLabel2: TQRLabel;
    QRLabel3: TQRLabel;
    QRDBText15: TQRDBText;
    QRDBText17: TQRDBText;
    QRDBText19: TQRDBText;
    QRDBText13: TQRDBText;
    QRLabel46: TQRLabel;
    QRLabel47: TQRLabel;
    TitleBand1: TQRBand;
    QRShape1: TQRShape;
    QRShape2: TQRShape;
    QRShape3: TQRShape;
    QRShape4: TQRShape;
    QRShape5: TQRShape;
    QRShape6: TQRShape;
    QRLabel4: TQRLabel;
    QRLabel5: TQRLabel;
    QRLabel6: TQRLabel;
    QRShape7: TQRShape;
    QRShape8: TQRShape;
    QRLabel7: TQRLabel;
    QRLabel8: TQRLabel;
    QRLabel11: TQRLabel;
    QRLabel12: TQRLabel;
    QRDBText1: TQRDBText;
    QRDBText2: TQRDBText;
    QRDBText4: TQRDBText;
    QRDBText6: TQRDBText;
    QRDBText3: TQRDBText;
    QRShape9: TQRShape;
    QRLabel9: TQRLabel;
    QRDBText5: TQRDBText;
    QRShape17: TQRShape;
    QRShape22: TQRShape;
    QRShape23: TQRShape;
    QRShape24: TQRShape;
    QRShape25: TQRShape;
    QRShape26: TQRShape;
    QRShape27: TQRShape;
    QRShape10: TQRShape;
    QRShape11: TQRShape;
    QRShape12: TQRShape;
    QRShape13: TQRShape;
    QRShape18: TQRShape;
    QRShape19: TQRShape;
    QRLabel10: TQRLabel;
    QRLabel13: TQRLabel;
    QRLabel14: TQRLabel;
    QRLabel15: TQRLabel;
    QRLabel16: TQRLabel;
    QRLabel17: TQRLabel;
    QRLabel18: TQRLabel;
    QRLabel19: TQRLabel;
    QRLabel20: TQRLabel;
    QRLabel21: TQRLabel;
    QRLabel22: TQRLabel;
    QRLabel23: TQRLabel;
    QRLabel24: TQRLabel;
    QRLabel32: TQRLabel;
    QRLabel33: TQRLabel;
    QRLabel34: TQRLabel;
    QRLabel35: TQRLabel;
    QRLabel36: TQRLabel;
    QRShape20: TQRShape;
    QRLabel37: TQRLabel;
    QRLabel38: TQRLabel;
    QRLabel41: TQRLabel;
    QRLabel42: TQRLabel;
    QRLabel43: TQRLabel;
    QRLabel44: TQRLabel;
    QRLabel45: TQRLabel;
    QRLabel48: TQRLabel;
    QRLabel49: TQRLabel;
    QRLabel50: TQRLabel;
    QRLabel51: TQRLabel;
    QRLabel52: TQRLabel;
    QRLabel53: TQRLabel;
    QRLabel54: TQRLabel;
    QRLabel55: TQRLabel;
    QRLabel56: TQRLabel;
    QRLabel57: TQRLabel;
    QRLabel58: TQRLabel;
    QRLabel59: TQRLabel;
    QRLabel60: TQRLabel;
    QRLabel61: TQRLabel;
    QRShape28: TQRShape;
    QRLabel62: TQRLabel;
    QRLabel63: TQRLabel;
    QRDBText7: TQRDBText;
    QRDBText8: TQRDBText;
    QRDBText9: TQRDBText;
    QRDBText10: TQRDBText;
    QRDBText11: TQRDBText;
    QRDBText12: TQRDBText;
    QRDBText14: TQRDBText;
    QRDBText16: TQRDBText;
    QRDBText18: TQRDBText;
    QRDBText20: TQRDBText;
    QRDBText21: TQRDBText;
    QRDBText22: TQRDBText;
    QRDBText23: TQRDBText;
    QRDBText24: TQRDBText;
    QRDBText25: TQRDBText;
    QRDBText26: TQRDBText;
    QRDBText27: TQRDBText;
    QRDBText28: TQRDBText;
    QRDBText29: TQRDBText;
    QRDBText30: TQRDBText;
    QRDBText31: TQRDBText;
    QRDBText32: TQRDBText;
    QRDBText33: TQRDBText;
    QRDBText34: TQRDBText;
    QRDBText35: TQRDBText;
    QRMemo4: TQRMemo;
    QRDBText36: TQRDBText;
    QRDBText37: TQRDBText;
    QRDBText38: TQRDBText;
  private

  public

  end;

var
  IdearRpt: TIdearRpt;

implementation
uses unit2;
{$R *.DFM}

end.

{=========PrintT.dpr============}

library PrintT;

{%File 'Print.asp'}

uses
  ComServ,
  PrintT_TLB in 'PrintT_TLB.pas',
  Unit1 in 'Unit1.pas' {Print: CoClass},
  Unit2 in 'Unit2.pas' {CustomerData: TDataModule},
  Unit23 in 'Unit23.pas' {IdearRpt: TQuickRep};

exports
  DllGetClassObject,
  DllCanUnloadNow,
  DllRegisterServer,
  DllUnregisterServer;

{$R *.TLB}

{$R *.RES}

begin
     IdearRpt := TIdearRPt.Create(NiL);{必不可少}
end.

      第五步:编译并注册PrintT组件,即可在ASP代码中调用,调用示例如下:

<% Set DelphiASPObj = Server.CreateObject("PrintT.Print")
   DelphiASPObj.Print1

%>

    通过以上步骤,我们就顺利地利用Delphi开发出了具有报表功能的ASP组件了。当然,笔者只是介绍了一个简单的思路,希望能起到抛砖引玉之功效。