QuickReport通用报表打印程序

 

unit unit_QReport_lxr;
{
    模块名:通用报表打印
    作者:尹曙光
    开发日期:2004-07-15
    网址:www.sinoprise.com
    本程序适用于GNU协议
    下面是使用方法:
    procedure TFrm_sbcx_yin.BitBtn_xxxx_dyClick(Sender: TObject);
    var
       qp:TQReport_lxr;
    begin
       qp:=TQReport_lxr.Create(self);
       qp.bb_title.Caption:='设备类别查询';
       if qp.bb_prepare(ADOQuery_xxxx,false)
       then begin
            qp.Prepare;
            qp.Preview;
            qp.hide;
       end;
      qp.free;
    end;
}
interface

uses Windows, SysUtils, Messages, Classes, Graphics, Controls,
  StdCtrls, ExtCtrls, Forms, QuickRpt, QRCtrls, DB, ADODB,Dialogs;

type
  TQReport_lxr = class(TQuickRep)
    theDetailBand: TQRBand;
    TitleBand1: TQRBand;
    bb_title: TQRLabel;
    theColumnHeaderBand: TQRBand;
  private
     function GetSL():TQRShape;
     function GetHL():TQRShape;

  public
    function bb_prepare(objDataSet:TDataSet;haveFirstCol:boolean):boolean;
  end;

var
  QReport_lxr: TQReport_lxr;

implementation

{$R *.DFM}
file://竖线
function TQReport_lxr.GetSL():TQRShape;
begin
  result:=TQRShape.Create(self);
  result.Shape:=qrsVertLine;
  result.Width:=3;
end;
file://横线
function TQReport_lxr.GetHL():TQRShape;
begin
  result:=TQRShape.Create(self);
  result.Shape:=qrsHorLine;
  result.Height:=3;
end;

function TQReport_lxr.bb_prepare(objDataSet:TDataSet;haveFirstCol:boolean):boolean;
file://haveFirstCol:boolean;是否打印dataset中的第一列,true打印,false 不打印
var
     Field_Vertical_space_div_2,Field_Horizontal_space_div_2:integer; file://字段的垂直距离的1/2,字段的水平距离的1/2
     Field_width,Field_height:integer;//字段的宽度,字段的高度
     Columns_count:integer;//每行显示的字段个数
     Current_row,Current_col:integer;//现在输出第几行,列;首行,列为1,
     rp_left,rp_width:integer;//报表的开始左边距,报表的宽度
     aVerticalLinePosition:array of integer;//记录每列的分割线的位置
     hafn:integer;  file://字段输出列的开始序号

     qr:TQRShape;  file://在报表上划线
     la:TQRLabel ; file://字段表签
     te:TQRDBText; file://字段内容

     i:integer;//循环变量

begin

    file://数据集是否打开
    if  (objDataSet.Fields.Count<=0)then
    begin
         messagedlg('请您先做查询,再打印报表!',mtInformation,[mbyes],0);
         result:=false;
         exit;
    end;
    file://设置初始值
    Field_Vertical_space_div_2   :=5;                      file://字段的垂直距离的1/2
    Field_Horizontal_space_div_2 :=5;                      file://字段的水平距离的1/2
    Field_width                  :=100;                     file://字段的宽度
    Field_height                 :=17;                      file://字段的高度
    rp_width                     :=theDetailBand.Width;     file://报表的宽度
    rp_left                      :=0;                       file://报表的开始左边距
    Columns_count:=rp_width div (Field_width+Field_Horizontal_space_div_2*2);//每行显示的字段个数
    SetLength(aVerticalLinePosition,Columns_count+1);
    file://本页的记录集
    DataSet:=objDataSet;

    file://注意:是否放弃objDataSet中的第一列
    if haveFirstCol
    then hafn:=0
    else hafn:=1;

    file://输出字段表头
          file://上边的横线
    qr:=GetHL;
    qr.Parent:=theColumnHeaderBand;
    qr.Top:=0;
    qr.Left:=rp_left;
    qr.Width:=rp_width;

    aVerticalLinePosition[0]:=rp_left;

    Current_row:=1;//现在输出第几行,首行为1
    Current_col:=1;
    for i:=hafn  to objDataSet.FieldCount-1 do
    begin
        theColumnHeaderBand.Height:=current_row*(Field_height+Field_Vertical_space_div_2*2);
        file://计算字段表头的高度
        la:=TQRLabel.Create(self);
        la.Parent:=theColumnHeaderBand;
        la.Caption:=objDataSet.Fields[i].FieldName;
        la.Width:=Field_width;
        la.Height:=Field_height;
        la.Left:=(2*Current_col-1)*Field_Horizontal_space_div_2+(Current_col-1)*Field_width;
        la.Top:=Current_row*Field_Vertical_space_div_2+(Current_row-1)*Field_height;


        file://计算竖线的位置
        aVerticalLinePosition[Current_col]:=Current_col*(Field_width+Current_col*2);

        if Current_col=Columns_count
        then begin
          Current_col:=1;
          Current_row:=Current_row+1;
        end
        else  Current_col:=Current_col+1;
    end;

    file://修改最后一列的位置
    aVerticalLinePosition[high(aVerticalLinePosition)]:=rp_width;
    file://在表头上划竖线
    for i:=low(aVerticalLinePosition)  to high(aVerticalLinePosition)  do
    begin
        qr:=GetSL;
        qr.Parent:=theColumnHeaderBand;
        qr.Top:=0;
        qr.Left:=aVerticalLinePosition[i];
        qr.Height := theColumnHeaderBand.Height;
    end;
    file://报表部分上边的横线
    qr:=GetHL;
    qr.Parent:=theDetailBand;
    qr.Top:=0;
    qr.Left:=rp_left;
    qr.Width:=rp_width;

    Current_col:=1;
    Current_row:=1;

    for i:=hafn  to objDataSet.FieldCount-1 do
    begin
        file://计算内容部分的高度
        theDetailBand.Height:=current_row*(Field_height+Field_Vertical_space_div_2*2);
        te:=TQRDBText.Create(self);
        te.DataSet:=objDataSet;
        te.Parent:=theDetailBand;
        te.DataField:=objDataSet.Fields[i].FieldName;
        te.Width:=Field_width;
        te.Height:=Field_height;
        te.Left:=(2*Current_col-1)*Field_Horizontal_space_div_2+(Current_col-1)*Field_width;
        te.Top:=Current_row*Field_Vertical_space_div_2+(Current_row-1)*Field_height;
        te.AutoStretch:=true;
        if Current_col=Columns_count
        then begin
          Current_col:=1;
          Current_row:=Current_row+1;
        end
        else  Current_col:=Current_col+1;
     end;

      file://在内容部分上划竖线
    for i:=low(aVerticalLinePosition)  to high(aVerticalLinePosition)  do
    begin
        qr:=GetSL;
        qr.Parent:=theDetailBand;
        qr.Top:=0;
        qr.Left:=aVerticalLinePosition[i];
        qr.Height := theDetailBand.Height;
    end;
   qr:=GetHL;
   qr.Parent:=theDetailBand;
   qr.Top:= theDetailBand.Height;
   qr.Left:=rp_left;
   qr.Width:=rp_width;
   result:=true;
  end;
end.

本文原名:quickreport万能打印程序,此为修改版,主要增加了报表上的表格显示,程序 也进行了优化(www.sinoprise.com).

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值