交流: 用QuickReport动态打印DBGrid内容 源代码(请指正)(50分)

  • 主题发起人 主题发起人 Yang J.Q.
  • 开始时间 开始时间
Y

Yang J.Q.

Unregistered / Unconfirmed
GUEST, unregistred user!
虽然最近在试用Rave作报表,但仍想用QuickReport来打印。
以下是我编写的用于打印DBGrid内容的源程序。想在下学期的教学中作为一个小例子讲授。
本想只需传递一个DBGrid即可(PreviewIt与IgnoreWidth均提供了缺省值),完全在函数中动态创建QuickReport。但
在 创 建 DetailBand 时, 无 法 在 程 序 中 指 定 其 BandType 为 rbDetail(编译出错),
又无QuickRep源码。不得不在调用方设计阶段放置一QuickRep控件及Title,Detail两QRBand在传入下述函数中。
请帮忙解决或就下述程序提出意见或指正。先谢过。
//通过QuickReport打印DBGrid(对于超出纸张宽度的Column不打印)
function PrintDBGrid(Grid: TDBGrid;
BandTitle, BandDetail: TQRBand;
PreviewIt: Boolean=True;
IgnoreWidth: integer=10): integer;
//PreviewIt: 为True时显示预览,为False时直接打印
//IgnoreWidth: 表示Grid中宽度小于此值的Column不予打印。
//返回打印记录数
var
I, iLeft, iTop: integer;
DataSetActive: Boolean;
txtSize: SIZE;
aLabel: TQRLabel;
aField: TQRDBText;
const
csTitlePrefix: string = '__ColumnTitle';
//ColumnHeader的QRLabel名前缀
csDataPrefix: string = '__DataField';
//QRDBText名前缀
begin
Result := 0;
if not Assigned(Grid.DataSource) then
Exit;
if not Assigned(Grid.DataSource.DataSet) then
Exit;
if not (BandTitle.Parent is TQuickRep) then
Exit;
if not (BandDetail.Parent is TQuickRep) then
Exit;
if not (BandTitle.Parent = BandDetail.Parent) then
Exit;
DataSetActive := Grid.DataSource.DataSet.Active;
if not DataSetActive then
Grid.DataSource.DataSet.Open;
//激活数据集
try
if Grid.Columns.Count = 0 then
Exit;
if Grid.DataSource.DataSet.RecordCount = 0 then
Exit;
for I := BandTitle.ControlCount-1do
wnto 0do
//释放已有QRLabel s
if (BandTitle.Controls is TQRLabel) and
(Pos(csTitlePrefix, BandTitle.Controls.Name)= 1) then
begin
aLabel := BandTitle.Controls as TQRLabel;
FreeAndNil(aLabel);
end;
for I := BandDetail.ControlCount-1do
wnto 0do
//释放已有QRDBText s
if (BandDetail.Controls is TQRDBText) and
(Pos(csDataPrefix, BandDetail.Controls.Name)= 1) then
begin
aField := BandDetail.Controls as TQRDBText;
FreeAndNil(aField);
end;
iLeft := 0;
for I := 0 to Grid.Columns.Count-1do
if Grid.Columns.Items.Width < IgnoreWidth then
Continue
else
with TQRLabel.Create(BandTitle)do
begin
Parent := BandTitle;
Left := iLeft;
Width := Grid.Columns.Items.Width;
iLeft := iLeft + Width;
if iLeft > BandTitle.ClientWidth then
Break;
Name := csTitlePrefix + IntToStr(I);
Caption := Grid.Columns.Items.Title.Caption;
Alignment := Grid.Columns.Items.Title.Alignment;
Top := BandTitle.ClientHeight - Height - 2;
end;
iLeft := 0;
GetTextExtentPoint(GetDC(BandDetail.ParentReport.Handle),
'Wg', 2, txtSize);
iTop := (BandDetail.ClientHeight - txtSize.cy) div 2;
for I := 0 to Grid.Columns.Count-1do
if Grid.Columns.Items.Width < IgnoreWidth then
Continue
else
with TQRDBText.Create(BandDetail)do
begin
Parent := BandDetail;
Left := iLeft;
Width := Grid.Columns.Items.Width;
iLeft := iLeft + Width;
if iLeft > BandDetail.ClientWidth then
Break;
Name := csDataPrefix + IntToStr(I);
Top := iTop;
DataSet := Grid.DataSource.DataSet;
DataField := Grid.Columns.Items.FieldName;
end;
if not Assigned((BandDetail.Parent as TQuickRep).DataSet) then
(BandDetail.Parent as TQuickRep).DataSet := Grid.DataSource.DataSet;
try
if PreviewIt then
BandDetail.ParentReport.Preview
else
BandDetail.ParentReport.Print;
Result := Grid.DataSource.DataSet.RecordCount;
finally
BandDetail.ParentReport.Visible := False;
end;
finally
if not DataSetActive then
Grid.DataSource.DataSet.Close;
end;
end;
 
网上去下载一个有源码的quickrep,写组件会好许多。
最起码他定义的一些类型,结构,都可以控制了。
我原来想通过程序修改quickrep纸张大小,没有源码就是没有办法。
 
to realLearning,
有些时候,与其写组件,我更乐于在Delphi程序中直接Uses实现相应功能的Unit文件而勿需安装相应组件。
我去找找源码。谢谢。
 
uses QRPrntr;
procedure TForm1.Button1Click(Sender: TObject);
begin
with TQRBand.Create(self)do
begin
Parent := QuickRep1;
QuickRep1.Bands.HasDetail := false;
BandType := rbDetail;
end;
end;
 
Yang J.Q.
你怎么教这些{打印DBGrid内容}
你能告诉我 你在什么类型的学校教课吗?
 
BandType 为 rbDetail(编译出错),
的方法
在 C:/Program Files/Borland/Delphi6/Lib中搜索 Qr.* 包含文字 “rbDetail”
 
谢谢各位,问题已解决。
现附上修正后的源码。
只需传递一个DBGrid参数即可打印。也可根据自己意愿再作改动。
to hfghfghfg,
教一门课叫《课程设计》的课。

//通过QuickReport打印DBGrid(对于超出纸张宽度的Column不打印)
function PrintDBGrid(Grid: TDBGrid;
PreviewIt: Boolean = True;
IgnoreWidth: integer=10): integer;
//PreviewIt: 为True时显示预览,为False时直接打印
//IgnoreWidth: 表示Grid中宽度小于此值的Column不予打印。
//返回打印记录数
var
I, iLeft, iTop: integer;
DataSetActive: Boolean;
txtSize: SIZE;
Qrep: TQuickRep;
BandTitle, BandDetail: TQRBand;
aLabel: TQRLabel;
aField: TQRDBText;
//const
// csTitlePrefix: string = '__ColumnTitle';
//ColumnHeader的QRLabel名前缀
// csDataPrefix: string = '__DataField';
//QRDBText名前缀
begin
Result := 0;
if not Assigned(Grid.DataSource) then
Exit;
if not Assigned(Grid.DataSource.DataSet) then
Exit;
Qrep := TQuickRep.Create(Grid.Parent);
Qrep.DataSet := Grid.DataSource.DataSet;
BandTitle := TQRBand.Create(Qrep);
BandTitle.Parent := Qrep;
BandTitle.BandType := rbTitle;
BandTitle.Frame.DrawBottom := True;
BandDetail := TQRBand.Create(Qrep);
BandDetail.Parent := Qrep;
BandDetail.BandType := rbDetail;
DataSetActive := Grid.DataSource.DataSet.Active;
if not DataSetActive then
Grid.DataSource.DataSet.Open;
try
iLeft := 0;
for I := 0 to Grid.Columns.Count-1do
if Grid.Columns.Items.Width < IgnoreWidth then
Continue
else
with TQRLabel.Create(BandTitle)do
begin
Parent := BandTitle;
Left := iLeft;
Width := Grid.Columns.Items.Width;
iLeft := iLeft + Width;
if iLeft > BandTitle.ClientWidth then
Break;
// Name := csTitlePrefix + IntToStr(I);
Caption := Grid.Columns.Items.Title.Caption;
Alignment := Grid.Columns.Items.Title.Alignment;
Top := BandTitle.ClientHeight - Height - 2;
end;
iLeft := 0;
GetTextExtentPoint(GetDC(BandDetail.ParentReport.Handle),
'Wg', 2, txtSize);
iTop := (BandDetail.ClientHeight - txtSize.cy) div 2;
for I := 0 to Grid.Columns.Count-1do
if Grid.Columns.Items.Width < IgnoreWidth then
Continue
else
with TQRDBText.Create(BandDetail)do
begin
Parent := BandDetail;
Left := iLeft;
Width := Grid.Columns.Items.Width;
iLeft := iLeft + Width;
if iLeft > BandDetail.ClientWidth then
Break;
// Name := csDataPrefix + IntToStr(I);
Top := iTop;
DataSet := Grid.DataSource.DataSet;
DataField := Grid.Columns.Items.FieldName;
end;
try
if PreviewIt then
BandDetail.ParentReport.Preview
else
BandDetail.ParentReport.Print;
Result := Grid.DataSource.DataSet.RecordCount;
finally
BandDetail.ParentReport.Visible := False;
end;
finally
if not DataSetActive then
Grid.DataSource.DataSet.Close;
Qrep.Free;
end;
end;
 
后退
顶部