谁有用QR打印query查询结果的源码,让我学习一下! (100分)

Z

zhenp

Unregistered / Unconfirmed
GUEST, unregistred user!
谁有用QR打印query查询结果的源码,让我学习一下!
我想学习QR,谁能帮我
我的email:
yachuan8@sina.com
 
放一个QRBand1在QuickRep1上,设置QRBand1.bandtype:=rbdetail
在QRBand1上QRDBText1设置QRDBText1.dataset为你的query,然后在
你的QRDBText1的datafield中选择字段名,就可以了
 
如果我用的是动态查询怎么处理
 
动态生成控件,动态设置控件属性?
如果你的查询最终有个最大值,一个简单的方法:
可以在设计时先生成,只不过此时是隐藏而已,然后再在需要时设置其属性为visible
(我没有试过)
 
那位大虾能帮我?:(
 
难道没有人吗
 
请查收!已发!
 
我以前已经有做过此类的问题,但在控制每个字段的宽度时,做得不是很好,如果有人修改了些程序别忘了通知我

unit GridRep;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
QuickRpt, ExtCtrls, Qrctrls, Db, printers, DBGrids, DBTables;
const MaxFields=20;
type
TPage=(A4Paper,A3Paper,A4SmallPaper,A5Paper,B4Paper,B5Paper,Custom);
TOrientation=(portrait,Landscape);
TfrmGridReport = class(TForm)
QuickRep1: TQuickRep;
ColumnHeaderBand1: TQRBand;
DetailBand1: TQRBand;
TitleBand1: TQRBand;
PageFooterBand1: TQRBand;
qrlblTitle: TQRLabel;
qrlblSummary: TQRLabel;
qrbLabel1: TQRLabel;
qrbLabel2: TQRLabel;
QRLabel1: TQRLabel;
procedure FormCreate(Sender: TObject);
Procedure CreateLabels;
Procedure FreeLabels;
procedure FormDestroy(Sender: TObject);
private //纸张宽度
FPage:TPage;
FOrientation:TOrientation;
//纸张方向
FDBGrid : TDBGrid;
FLabelWidthTag:Boolean;
FReportTitle : string;
FSubTitle : string;
FLable1 : string;
FLable2 : string;
FFieldList:TStrings;
FLength,FWidth:Extended;
public
RowCount : Integer;
MaxCols: integer;
ColumnLabels: array[0..MaxFields] of TQRLabel;
DataLabels: array[0..MaxFields] of TQRDBText;
//VerticalLine1:array[0..MaxFields] of TQRShape;
//VerticalLine2:array[0..MaxFields] of TQRShape;
SafeToRun : boolean;
end;

TGridRep=Class(TComponent)
private
GridReport : TfrmGridReport;
function GetDbGrid : TDbGrid ;
procedure SetDbGrid(value : TDbGrid);
function GetPageSize : TPage;
procedure SetPageSize(Value : TPage);
function GetOrientation : TOrientation;
procedure SetOrientation(value : TOrientation);
function GetLabelWidthTag : Boolean;
procedure SetLabelWidthTag(Value:Boolean);
function GetReportTitle : string;
procedure SetReportTitle (value : string);
function GetSubTitle : String;
procedure SetSubTitle(Value:String);
function GetLabel1:String;
procedure SetLabel1(Value:String);
function GetFieldList:TStrings;
procedure SetFieldList(Value:TStrings);
function GetLength:Extended;
procedure SetLength(Value:Extended);
function GetWidth:Extended;
procedure SetWidth(Value:Extended);
public
procedure Preview ;
procedure Print;
// procedure test;
constructor Create(Owner : TComponent) ;
Override;
destructor Destroy ;
Override;
published
property PageSize : TPage read GetPageSize write SetPageSize ;
property Orientation :TOrientation read GetOrientation write SetOrientation;//99-4-24
property Grid : TDbGrid read GetDbGrid write SetDbGrid;
property LabelWidth: boolean read GetLabelWidthTag write SetLabelWidthTag ;
property ReportTitle : string read GetReportTitle write SetReportTitle;
property SubTitle :String read GetSubTitle write SetSubTitle;
property PageHeadLabel1 : string read GetLabel1 write SetLabel1;
property FieldList:TStrings read GetFieldList Write SetFieldList;
property Length:Extended read GetLength write SetLength;
property Width:Extended read GetWidth write SetWidth;
end;

procedure Register;
var
frmGridReport: TfrmGridReport;
implementation
{$R *.DFM}
{procedure TGridRep.test;
begin
GridReport.Left:=1;
end;
}
function TGridRep.GetLength:Extended;
begin
result:=GridReport.FLength;
end;

procedure TGridRep.SetLength(Value:Extended);
begin
GridReport.FLength:=Value;
end;

function TGridRep.GetWidth:Extended;
begin
result:=GridReport.FWidth;
end;

procedure TGridRep.SetWidth(Value:Extended);
begin
GridReport.FWidth:=Value;
end;

function TGridRep.GetFieldList:TStrings;
begin
result:=GridReport.FFieldList;
end;

procedure TGridRep.SetFieldList(Value:TStrings);
begin
GridReport.FFieldList:=Value;
end;

function TGridRep.GetOrientation :TOrientation;
// TQRPage;
begin
result:=GridReport.FOrientation;
end;

procedure TGridRep.SetOrientation(Value :TOrientation);
//TQRPage) ;
begin
GridReport.FOrientation:=Value;
end;

function TGridRep.GetPageSize :TPage;
// TQRPage;
begin
result:=GridReport.FPage
end;

procedure TGridRep.SetPageSize(Value :TPage);
//TQRPage) ;
begin
GridReport.FPage:=Value;
end;

function TGridRep.GetLabelWidthTag : Boolean;
begin
result:=GridReport.FLabelWidthTag;
end;

procedure TGridRep.SetLabelWidthTag(Value:Boolean);
begin
GridReport.FLabelWidthTag:=Value;
end;

function TGridRep.GetReportTitle : String;
begin
Result:=GridReport.FReportTitle;
end;
procedure TGridRep.SetReportTitle(Value:String);
begin
GridReport.FReportTitle:=Value;
end;
function TGridRep.GetSubTitle : String;
begin
Result:=GridReport.FSubTitle;
end;
procedure TGridRep.SetSubTitle(Value:String);
begin
GridReport.FSubTitle:=Value;
end;

function TGridRep.GetLabel1 : String;
begin
Result:=GridReport.FLable1;
end;
procedure TGridRep.SetLabel1(Value:String);
begin
GridReport.FLable1:=Value;
end;

function TGridRep.GetDbGrid : TDbGrid ;
begin
result:=GridReport.FDBGrid;
end;

procedure TGridRep.SetDbGrid(value : TDbGrid);
begin
GridReport.FDBGrid:=Value;
end;

procedure TGridRep.Preview ;
begin
GridReport.CreateLabels;
with GridReportdo
begin
qrlblTitle.Caption:=FReportTitle;
qrlblSummary.Caption:=FSubTitle;
qrbLabel1.Caption:=FLable1;
qrbLabel2.Caption:=FLable2;
end;
GridReport.QuickRep1.Preview;
GridReport.FreeLabels;
end;

procedure TGridRep.Print;
begin
GridReport.CreateLabels;
with GridReportdo
begin
qrlblTitle.Caption:=FReportTitle;
qrlblSummary.Caption:=FSubTitle;
qrbLabel1.Caption:=FLable1;
qrbLabel2.Caption:=FLable2;
end;
GridReport.QuickRep1.Print;
GridReport.FreeLabels;
end;

constructor TGridRep.Create(Owner : TComponent) ;
begin
inherited Create(Owner);
GridReport:=TfrmGridReport.Create(Self);
end;

destructor TGridRep.Destroy ;
begin
GridReport.Free;
inherited Destroy;
end;

procedure TfrmGridReport.FormCreate(Sender: TObject);
begin
SafeToRun := false;
FFieldList:=TStringList.Create;
end;

Procedure TfrmGridReport.CreateLabels;
var
nIdx,nWidth: integer;
//计算当前值 nLeft,
iLeft:Integer;
//计算总值
TmpId,TmpMax:Integer;
i,iLength:integer;
begin
if FOrientation=portrait then
//确定纸张方向 99-4-24
QuickRep1.Page.Orientation := poportrait
else
QuickRep1.Page.Orientation:= poLandscape;
if FPage=Custom then
begin
QuickRep1.Page.Length:=FLength;
QuickRep1.Page.Width:=FWidth;
end;
nWidth:=0;
iLength:=0;
TmpId:=0 ;
TmpMax:=0;
while TmpId<FDBGrid.DataSource.DataSet.FieldCount-1do
begin
//if FDBGrid.DataSource.DataSet.Fields[TmpId].Visible=true then
if FDBGrid.DataSource.DataSet.Fields[TmpId].Tag=1 then
TmpMax:=TmpMax+1;
MaxCols:=TmpMax;
Inc(TmpId);
if MaxCols>MaxFields then
MaxCols:=MaxFields;
end;

if FLabelWidthTag=True then
nWidth:=QuickRep1.Bands.ColumnHeaderBand.Width div MaxCols
else
begin
//计算打印长度是澡超长
for i:=0 to FDBGrid.DataSource.DataSet.FieldCount-1do
if FDBGrid.DataSource.DataSet.Fields.Tag=1 then
iLength:=iLength+FDBGrid.DataSource.DataSet.Fields.DisplayWidth*8;
if iLength>QuickRep1.Bands.ColumnHeaderBand.Width then
if MessageDlg('打印纸张超出范围,是否继续?',
mtConfirmation, [mbYes, mbNo], 0) <> mrYes then
abort;
end;
if (QuickRep1.Bands.HasDetail) and (QuickRep1.Bands.HasColumnHeader) then
begin
QuickRep1.DataSet:=FDBGrid.DataSource.DataSet;
nIdx := 0;
iLeft:=0;
while nIdx <FDBGrid.DataSource.DataSet.FieldCount-1do
begin
//if (FDBGrid.DataSource.DataSet.Fields[nIdx].Visible=True)
if (FDBGrid.DataSource.DataSet.Fields[nIdx].Tag=1)
then
begin
if FLabelWidthTag=True then
begin
if nIdx=0 then
iLeft:=0
else
iLeft:=iLeft+nWidth;
end else
begin
if nIdx=0 then
iLeft:=0
else
iLeft:=iLeft+nWidth;
nWidth:=FDBGrid.DataSource.DataSet.Fields[nIdx].DisplayWidth*8;
end;
ColumnLabels[nIdx] := TQRLabel.Create(Self);
With ColumnLabels[nIdx]do
begin
Parent := QuickRep1.Bands.ColumnHeaderBand;
Height := Parent.Height;
Autosize := True;
top:=2;
left:=iLeft+6;
width:=nWidth;
Caption := FDBGrid.DataSource.DataSet.Fields[nIdx].DisplayLabel;
{ VerticalLine1[nIdx]:=TQRShape.Create(Self);
with VerticalLine1[nIdx]do
begin
Parent := QuickRep1.Bands.ColumnHeaderBand;
Height := Parent.Height;
Autosize := True;
top:=0;
Width := 1;
left:=iLeft;
if (nIdx=0) then
Enabled:=False
else
Enabled:=True;
end;
}
end;
DataLabels[nIdx] := TQRDBText.Create(Self);
with DataLabels[nIdx]do
begin
DataLabels[nIdx].DataSet:=FDBGrid.DataSource.DataSet;
DataLabels[nIdx].DataField:=FDBGrid.DataSource.DataSet.Fields[nIdx].FieldName;
Parent := QuickRep1.Bands.DetailBand;
Height := Parent.Height;
top:=2;
width:=nWidth;
left:=iLeft+5;
{VerticalLine2[nIdx]:=TQRShape.Create(Self);
with VerticalLine2[nIdx]do
begin
Parent := QuickRep1.Bands.DetailBand;
Height := Parent.Height;
Autosize := True;
top:=0;
Width := 1;
left:=iLeft;
if (nIdx=0) then
Enabled:=False
else
Enabled:=True;
end;
}
end;
end;
Inc(nIdx);
end;
SafeToRun:= True;
end;
end;

Procedure TfrmGridReport.FreeLabels;
var
nIdx: integer;
begin
SafeToRun := false;
for nIdx := 0 to MaxFieldsdo
begin
ColumnLabels[nIdx].Free;
DataLabels[nIdx].Free;
// VerticalLine1[nIdx].Free;
// VerticalLine1[nIdx]:=nil;
// VerticalLine2[nIdx].Free;
// VerticalLine2[nIdx]:=nil;
ColumnLabels[nIdx]:=nil;
DataLabels[nIdx]:=nil;
end;
end;

procedure Register;
begin
RegisterComponents('MyLibs', [TGridRep]);
end;

procedure TfrmGridReport.FormDestroy(Sender: TObject);
begin
FFieldList.Free;
end;

end.
 
接受答案了.
 
顶部