别这么懒,看看这下面的程序,你稍加修改,就可以一劳永逸的。
我的程序中,先建立了一个Qr报表窗口做模板,这样只是动态的建立报表控件了,
程序可以更好实现。
(来自TommStudio的一位热心的朋友)
delphi的报表不好做是大家都知道的!尤其是做边框的时候真的是让人望而却步。如果用qrdbtext的frames属性去画出来又太难看。思来想去,我就决定自己做一个自动创报表的function,现在function已经做好的,但是有些不足之处:
第一:预览的时候,边线看不到,只有放大之后才能看到漂亮的边框线。
第二:创出来的报表只有报表标题,字段标题,字段内容,制表日期,页次,等基本报表内容;
第三:当table或query的字段数太多,字段太长的时候,页面的宽度不能自动适应!
我所发现的不足就是这么多了!希望各位高手能把下面的这段源代码拿回去修改
修改,让它完善起来,然后贴回到这里,与所有喜欢delphi的朋友一起来分享这个成果!以后我们就不用整天埋怨delphi的报表不好了!
在包含这个函数的单元里引用了以下几个单元(除那些基本的单元外):QuickRpt,printers, Qrctrls,qrprntr,dbtables
function CreateRePort(Ddataset:TDBDataset;Title:string):Tquickrep;
var qreport:Tquickrep;
qrheader,qrdetail,qrfooter:Tqrband;
qsTitle: TQRSysData;
qrlb:TQRLabel;
qrtx:Tqrdbtext;
qrhshape,qrdshape:Tqrshape;
qrcanvas:Tform;
i,lbleft,lbwidth:integer;
begin
lbleft:=0;
qreport:=Tquickrep.Create(application);
qrcanvas:=Tform.Create(nil);
with qreportdo
begin
name:='qreport1';
reporttitle:=Title;
dataset:=Ddataset;
end;
{创建页眉}
qrheader:=Tqrband.create(qreport);
with qrheaderdo
begin
bandtype:=rbpageheader;
parent:=qreport;
height:=60;
qsTitle := TQRSysData.Create(qrHeader);
with qsTitledo
begin
parent:=qrheader;
autosize:=true;
data:=qrsReportTitle;
aligntoband:=true;
alignment:=tacenter;
font.Charset:=GB2312_Charset;
qsTitle.Font.Name:='黑体';
font.Size:=18;
top:=0;
end;
end;
{创建页脚}
qrfooter:=Tqrband.Create(qreport);
with qrfooterdo
begin
parent:=qreport;
bandtype:=rbpagefooter;
end;
{创建明细段}
qrdetail:=Tqrband.Create(qreport);
with qrDeTaildo
begin
parent:=qreport;
bandtype:=rbDetail;
qrcanvas.Canvas.Font:=font;
height:=qrcanvas.Canvas.TextHeight('W')+3;
end;
{创建标题}
for i:=0 to ddataset.Fields.Count-1do
begin
if ddataset.fields.Visible then
begin
qrhshape:=Tqrshape.Create(qrheader);
qrlb:=Tqrlabel.Create(qrheader);
with qrlbdo
begin
parent:=qrheader;
qrcanvas.Font:=font;
name:='qrib'+inttostr(i);
caption:=ddataset.Fields.DisplayLabel;
Alignment:=tacenter;
width:=ddataset.Fields.DisplayWidth*qrcanvas.Canvas.TextWidth('W');
top:=qrheader.height-qrcanvas.Canvas.TextHeight('W');
left:=lbleft+1;
lbwidth:=width;
end;
with qrhshapedo
begin
parent:=qrheader;
name:='qrhshape'+inttostr(i);
width:=1;
shape:=qrsVertLine;
top:=qrheader.height-qrcanvas.Canvas.TextHeight('W');
height:=qrheader.height;
left:=lbleft;
end;
{创建字段}
qrtx:=Tqrdbtext.Create(qrdetail);
qrdshape:=Tqrshape.Create(qrdetail);
with qrtxdo
begin
parent:=qrDetail;
dataset:=ddataset;
qrcanvas.Font:=font;
name:='qrib'+inttostr(i);
caption:=ddataset.Fields.DisplayLabel;
Alignment:=ddataset.fields.Alignment;
width:=ddataset.Fields.DisplayWidth*qrcanvas.canvas.TextWidth('W');
height:=qrcanvas.Canvas.TextHeight('W')+2;
top:=1;
left:=lbleft;
datafield:=ddataset.Fields.FieldName;
end;
{字段左线}
with qrdshapedo
begin
parent:=qrdetail;
name:='qrhshape'+inttostr(i);
width:=1;
shape:=qrsVertLine;
top:=0;
height:=qrdetail.height;
left:=lbleft;
end;
lbleft:=lbleft+lbwidth+1;
end;
end;
{标题最后一根线}
with Tqrshape.Create(qrheader)do
begin
parent:=qrheader;
width:=1;
shape:=qrsVertLine;
top:=qrheader.height-qrcanvas.Canvas.TextHeight('W');
height:=qrheader.height-1;
left:=lbleft;
end;
{字段最后一根线}
with Tqrshape.Create(qrdetail)do
begin
parent:=qrdetail;
width:=1;
shape:=qrsVertLine;
top:=0;
height:=qrdetail.height;
left:=lbleft;
end;
{标题上线}
with Tqrshape.Create(qrheader)do
begin
parent:=qrheader;
width:=lbleft-1;
shape:=qrsHorLine;
top:=qrheader.height-qrcanvas.Canvas.TextHeight('W')-1;
height:=1;
left:=1;
end;
{标题下线}
with Tqrshape.Create(qrheader)do
begin
parent:=qrheader;
width:=lbleft-1;
shape:=qrsHorLine;
top:=qrheader.height;
height:=1;
left:=1;
end;
{字段下线}
with Tqrshape.Create(qrdetail)do
begin
parent:=qrdetail;
width:=lbleft-1;
shape:=qrsHorLine;
top:=qrdetail.Height-1;
height:=1;
left:=1;
end;
{页面是横还是竖}
if lbleft>qrheader.Width then
begin
with qreportdo
begin
Page.Orientation:=poLandscape;
Page.RightMargin:=max(10,(Page.width-(page.width-page.rightmargin-page.leftmargin)/qrheader.Width*lbleft)/2-5);
Page.leftMargin:=page.RightMargin+5;
end;
end else
begin
with qreportdo
begin
Page.Orientation:=poPortrait;
Page.RightMargin:=max(10,(Page.width-(page.width-page.rightmargin-page.leftmargin)/qrheader.Width*lbleft)/2-5);
Page.leftMargin:=page.RightMargin+5;
end;
end;
result:=qreport;
qsTitle.Font.Name:='黑体';
qreport.Preview ;
qreport.Free;
end;
//动态生成各QRLabel和QRDBtext,用QuickRep做动态报表
procedure TForm8.BitBtn2Click(Sender: TObject);
var
AQRLabel: TQRLabel;
AQRDBText: TQRDBText;
i:integer;
aLeft : Integer;
aTop: integer;
begin
try
FormP := TformP.Create(self);
with Formpdo
begin
aLeft := 5;
aTop := 10;
QRLabel1.Caption := '标题';//自行设定标题
for i:= 0 to Query1.FieldCount - 1do
begin
aQRLabel := TQRLabel.Create(self);
aQRLabel.ParentReport := QuickRep1;
aQRLabel.Parent := ColumnHeaderBand1;
aQRLabel.Left := aLeft ;
aQRlabel.Top := aTop;
aQRLabel.Caption := Query1.Fields.FieldName;
aQRDBText := TQRDBText.Create(self);
aQRDBText.ParentReport := QuickRep1;
aQRDBText.Parent := DetailBand1;
aQRDBText.DataSet := QuickRep1.DataSet;
aQRDBText.DataField := Query1.Fields.FieldName;
aQRDBText.Left := aLeft;
aQRDBText.Top := aTop;
aLeft := aLeft + 60;//根据需要设定,改变数值大小可调距离
end;
QuickRep1.Preview;
end;
finally
FormP.Free;
end;
end;