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;
以下是我编写的用于打印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;