建立两个窗体mainform和repform。在mainform上放置edit1、datasource1、query1、dbgrid1、button1(caption为“提取数据”)和button2(caption为“预览报表”),将datasource1.dataset设定为query1,将dbgrid1的datasource设定为datasource1。在repform上放置Quickrep1,并至少将Quickrep1.bands.hascolumnband和Quickrep1.bands.hasdetailband设为true。 编程实现 运行时,在edit1中输入正确的 SQL语句。点击“提取数据”按钮,将相应记录显示在 dbgrid1中。具体的程序代码如下: procedure Tmainform.button1click(sender:TObject); var s:string; begin s:=edit1.text; with query1 do begin close; sql.clear; sql.add(s); open; end; end; 点击“预览报表”按钮,可以看到形成的报表,如果对宽度摆放还有些不满意,可以关闭预览窗口后重新调整。具体的程序代码如下: procedure Tmainform.button2click(sender:TObject); var leftv,i:integer; //leftv为报表起始位置 cheader:Tqrlabel; detailtext:Tqrdbtext; …… begin leftv:=0; for i:=0 to dbgrid1.columns.Count-1 do begin cheader:=TQrlabel.Create(repform); with repform.cheader do begin parent:=repform.columnheaderband1; caption:=dbgrid1.columns[i].fieldname; width:=dbgrid1.columns[i].width; height:=repform.columnheaderband1.height; left:=leftv+2; top:=5; end; ……//在leftv 处画表格竖线 leftv:=dbgrid1.columns[i].width+leftv; end; ……//画表头栏边框。起始位为0,总宽度为leftv,也就是dbgrid1的各列宽度之和 leftv:=0; //显示记录内容 for i:=0 to dbgrid1.columns.Count-1 do begin detailtext:=TQrdbtext.create(repform); with repform.detailtext do begin parent:=repform.detailband1; dataset:=query1; datafield:=dbgrid1.columns[i].fieldname; width:=dbgrid1.columns[i].width; height:=repform.detailband1.height; left:=leftv+2; top:=5; end; ……//在leftv 处画表格竖线 leftv:=dbgrid1.columns[i].width+leftv; end; ……// 画明晰栏边框。 repform.quickrep1.preview();//报表预览 end;
procedure TForm1.Button2Click(Sender: TObject); var pl,pr,pt,pb:integer;//页边距 px,py:integer;//打印机分辨率 ps:integer;//打印机与显示器分辨率之比 lh:integer;//行高 i:integer; x,y:integer;//打印坐标 rowb,colb:integer; //行距、行距 begin //初始化页面 pl:=spinedit1.Value; pr:=spinedit2.Value; pt:=spinedit3.Value; pb:=spinedit4.Value; rowb:=spinedit6.Value;//行距 colb:=spinedit7.Value;//列距 //将获得的打印机分辨率由厘米转换为英寸 px:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/2.54); py:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSY)/2.54); //将屏幕分辨率转换为打印机的比例 ps:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX) /Screen.PixelsPerInch); //设置打印方向 if combobox1.Text='横向打印' then printer.Orientation:=poPortrait; else printer.Orientation:=poLandscape; //获得打印的字体和大小 printer.Canvas.Font.Name:=combobox2.Text; printer.canvas.Font.Size:=spinedit5.Value; printer.canvas.Font.Color:=clred; printer.Canvas.Pen.Color:=clblue; //设置行高 lh:=printer.canvas.textheight(dbgrid1.Font.Name); //打印的起始位置(打印机画布上的位置) x:=PX*pl; y:=PY*pt; //打印DBGrid1中的内容 if (table1.Active=true) and (table1.RecordCount>0) then begin //设置一打印机任务标题 printer.Title:='DBGrid内容打印'; with printer do begin BeginDoc; table1.First; while not table1.Eof do begin for i:=0 to DBGrid1.FieldCount-1 do begin //设定打印的宽度 if (x+DBGrid1.Columns.Items[i].Width*ps) <=(PageWidth-ps*pr) then begin //画出表格线 Printer.Canvas.Rectangle(x,y,x+dbgrid1.Columns.Items[i].Width*ps,y+lh); //如果为第一行时,打印DBGrid1确的标题 if y=PY*pt then Canvas.TextOut(x+rowb,y+colb, DBGrid1.Columns[i].Title.Caption) else //其它行,打印每行的内容 Printer.Canvas.TextOut(x+rowb,y+colb, DBGrid1.Fields[i].asString); end; //下一列的横坐标,下一列的纵坐标不变 x:=x+dbgrid1.Columns.Items[i].Width*ps; end; //打印下一记录 if not (y=py*pt) then table1.next; x:=px*pl; //打印下一行时,横坐标回到行首 y:=y+lh; //打印满一页时,新建一页 if (y+lh)>(PageHeight-py*pb) then begin NewPage; x:=px*pl; y:=py*pt; end; end; EndDoc; end; table1.First;//回到DBGrid1首行 Application.MessageBox('已打印完成','打印',32); end; end;procedure TForm1.FormActivate(Sender: TObject); begin //获得打印可选字体,并设置初始字体为DBGrid1的字体,字号 combobox2.Items:=printer.Fonts; combobox2.Text:=dbgrid1.Font.Name; spinedit5.value:=dbgrid1.Font.Size; table1.DatabaseName:=ExtractFilePath(Application.ExeName); table1.open; end; end.
我最近才做完,效果还可以
尤其解决了自动换行的问题
需要的话
[email protected]
编程实现
运行时,在edit1中输入正确的 SQL语句。点击“提取数据”按钮,将相应记录显示在 dbgrid1中。具体的程序代码如下:
procedure Tmainform.button1click(sender:TObject);
var s:string;
begin
s:=edit1.text;
with query1 do
begin
close;
sql.clear;
sql.add(s);
open;
end;
end;
点击“预览报表”按钮,可以看到形成的报表,如果对宽度摆放还有些不满意,可以关闭预览窗口后重新调整。具体的程序代码如下:
procedure Tmainform.button2click(sender:TObject);
var leftv,i:integer; //leftv为报表起始位置
cheader:Tqrlabel;
detailtext:Tqrdbtext;
……
begin
leftv:=0;
for i:=0 to dbgrid1.columns.Count-1 do
begin
cheader:=TQrlabel.Create(repform);
with repform.cheader do
begin
parent:=repform.columnheaderband1;
caption:=dbgrid1.columns[i].fieldname;
width:=dbgrid1.columns[i].width;
height:=repform.columnheaderband1.height; left:=leftv+2;
top:=5;
end;
……//在leftv 处画表格竖线
leftv:=dbgrid1.columns[i].width+leftv;
end;
……//画表头栏边框。起始位为0,总宽度为leftv,也就是dbgrid1的各列宽度之和
leftv:=0;
//显示记录内容
for i:=0 to dbgrid1.columns.Count-1 do
begin
detailtext:=TQrdbtext.create(repform);
with repform.detailtext do
begin
parent:=repform.detailband1;
dataset:=query1;
datafield:=dbgrid1.columns[i].fieldname;
width:=dbgrid1.columns[i].width;
height:=repform.detailband1.height;
left:=leftv+2;
top:=5;
end;
……//在leftv 处画表格竖线
leftv:=dbgrid1.columns[i].width+leftv;
end;
……// 画明晰栏边框。
repform.quickrep1.preview();//报表预览
end;
var
pl,pr,pt,pb:integer;//页边距
px,py:integer;//打印机分辨率
ps:integer;//打印机与显示器分辨率之比
lh:integer;//行高
i:integer;
x,y:integer;//打印坐标
rowb,colb:integer; //行距、行距
begin
//初始化页面
pl:=spinedit1.Value;
pr:=spinedit2.Value;
pt:=spinedit3.Value;
pb:=spinedit4.Value;
rowb:=spinedit6.Value;//行距
colb:=spinedit7.Value;//列距
//将获得的打印机分辨率由厘米转换为英寸
px:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/2.54);
py:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSY)/2.54);
//将屏幕分辨率转换为打印机的比例
ps:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)
/Screen.PixelsPerInch);
//设置打印方向
if combobox1.Text='横向打印' then
printer.Orientation:=poPortrait;
else
printer.Orientation:=poLandscape;
//获得打印的字体和大小
printer.Canvas.Font.Name:=combobox2.Text;
printer.canvas.Font.Size:=spinedit5.Value;
printer.canvas.Font.Color:=clred;
printer.Canvas.Pen.Color:=clblue;
//设置行高
lh:=printer.canvas.textheight(dbgrid1.Font.Name);
//打印的起始位置(打印机画布上的位置)
x:=PX*pl;
y:=PY*pt;
//打印DBGrid1中的内容
if (table1.Active=true) and (table1.RecordCount>0) then
begin
//设置一打印机任务标题
printer.Title:='DBGrid内容打印';
with printer do
begin
BeginDoc;
table1.First;
while not table1.Eof do
begin
for i:=0 to DBGrid1.FieldCount-1 do
begin
//设定打印的宽度
if (x+DBGrid1.Columns.Items[i].Width*ps)
<=(PageWidth-ps*pr) then
begin
//画出表格线
Printer.Canvas.Rectangle(x,y,x+dbgrid1.Columns.Items[i].Width*ps,y+lh);
//如果为第一行时,打印DBGrid1确的标题
if y=PY*pt then
Canvas.TextOut(x+rowb,y+colb,
DBGrid1.Columns[i].Title.Caption)
else
//其它行,打印每行的内容
Printer.Canvas.TextOut(x+rowb,y+colb,
DBGrid1.Fields[i].asString);
end;
//下一列的横坐标,下一列的纵坐标不变
x:=x+dbgrid1.Columns.Items[i].Width*ps;
end;
//打印下一记录
if not (y=py*pt) then
table1.next;
x:=px*pl; //打印下一行时,横坐标回到行首
y:=y+lh;
//打印满一页时,新建一页
if (y+lh)>(PageHeight-py*pb) then
begin
NewPage;
x:=px*pl;
y:=py*pt;
end;
end;
EndDoc;
end;
table1.First;//回到DBGrid1首行
Application.MessageBox('已打印完成','打印',32);
end;
end;procedure TForm1.FormActivate(Sender: TObject);
begin
//获得打印可选字体,并设置初始字体为DBGrid1的字体,字号
combobox2.Items:=printer.Fonts;
combobox2.Text:=dbgrid1.Font.Name;
spinedit5.value:=dbgrid1.Font.Size;
table1.DatabaseName:=ExtractFilePath(Application.ExeName);
table1.open;
end;
end.