uses
DBGrids, QuickRpt, DB, Qrctrls,qrprntr,printers;
procedure PrintDBGrid(DBGrid: TDBGrid;
pTitle: string);
const
VER_MSG = '显示信息';
VG_UnitName='公司名称';
var
aReport: TQuickRep;
i, aLeft: integer;
PageHeaderBand1: TQRBand;
ColumnHeaderBand1: TQRBand;
DetailBand1: TQRBand;
PageFooterBand1: TQRBand;
oldBmk: TBookMark;
begin
if not DBGrid.DataSource.DataSet.Active then
begin
ShowMessage('数据集没有打开!');
Exit;
end;
oldBmk := DBGrid.DataSource.DataSet.GetBookmark;
DBGrid.DataSource.DataSet.First;
aReport := TQuickRep.Create(nil);
PageHeaderBand1 := aReport.CreateBand(rbPageHeader);
ColumnHeaderBand1 := aReport.CreateBand(rbColumnHeader);
DetailBand1 := aReport.CreateBand(rbDetail);
PageFooterBand1 := aReport.CreateBand(rbPageFooter);
aReport.DataSet := DBGrid.DataSource.DataSet;
aReport.Font := DBGrid.Font;
DetailBand1.Height := 27;
with TQRLabel(PageHeaderBand1.AddPrintable(TQRLabel))do
begin
Caption := pTitle;
Top := 10;
Left := 0;
Font.Style := [fsBold];
Font.Size := 14;
Width := 0;
end;
aLeft := 0;
for i := 0 to DBGrid.FieldCount - 1do
aLeft := aLeft + DBGrid.Columns.Width + 10;
if aLeft > 728 then
aReport.Page.Orientation := poLandscape
else
aReport.Page.Orientation := poPortrait;
with TQRShape(PageHeaderBand1.AddPrintable(TQRShape))do
begin
Top := 37;
Left := 0;
Height := 2;
pen.Width := 2;
Width := PageHeaderBand1.Width;
end;
with TQRShape(PageFooterBand1.AddPrintable(TQRShape))do
begin
Top := 2;
Left := 0;
Height := 1;
pen.Width := 1;
Width := PageFooterBand1.Width;
end;
with TQRLabel(PageHeaderBand1.AddPrintable(TQRLabel))do
begin
Font.Size := 8;
Caption := VG_UnitName;
Top := PageHeaderBand1.height - Height - 5;
Left := PageHeaderBand1.Width - length(VER_MSG) * Font.Size - 5;
end;
with TQRSysData(PageFooterBand1.AddPrintable(TQRSysData))do
begin
Data := qrsDateTime;
Top := 10;
Left := 0;
end;
with TQRLabel(PageFooterBand1.AddPrintable(TQRLabel))do
begin
Caption := '第';
Top := 10;
Left := PageFooterBand1.Width - 380;
end;
with TQRSysData(PageFooterBand1.AddPrintable(TQRSysData))do
begin
Data := qrsPageNumber;
Top := 10;
Left := PageFooterBand1.Width - 360;
end;
with TQRLabel(PageFooterBand1.AddPrintable(TQRLabel))do
begin
Caption := '页';
Top := 10;
Left := PageFooterBand1.Width - 300;
end;
with TQRLabel(PageFooterBand1.AddPrintable(TQRLabel))do
begin
Caption := '记录总数:';
Top := 10;
Left := PageFooterBand1.Width - 180;
end;
with TQRSysData(PageFooterBand1.AddPrintable(TQRSysData))do
begin
Data := qrsDetailCount;
Top := 10;
Left := PageFooterBand1.Width - 100;
end;
aLeft := 0;
for i := 0 to DBGrid.FieldCount - 1do
begin
if DBGrid.Columns.Visible = false then
continue;
if aLeft + DBGrid.Columns.Width > PageHeaderBand1.Width then
Break;
with TQRShape(ColumnHeaderBand1.AddPrintable(TQRShape))do
begin
Height := 1;
Top := ColumnHeaderBand1.Height - 5;
Left := aLeft;
Width := DBGrid.Columns.Width;
end;
with TQRLabel(ColumnHeaderBand1.AddPrintable(TQRLabel))do
begin
AutoSize := false;
Width := DBGrid.Columns.Width;
Caption := DBGrid.Columns.Title.Caption;
Alignment := taCenter;
Top := 10;
Left := aLeft;
end;
with TQRDBText(DetailBand1.AddPrintable(TQRDBText))do
begin
DataSet := DBGrid.DataSource.DataSet;
DataField := DBGrid.Columns.FieldName;
Alignment := DBGrid.Columns.Alignment;
Top := 5;
Left := aLeft;
AutoSize := False;
Width := DBGrid.Columns.Width;
end;
aLeft := aLeft + DBGrid.Columns.Width + 10;
end;
aReport.Preview;
ColumnHeaderBand1.Free;
DetailBand1.Free;
PageFooterBand1.Free;
PageHeaderBand1.Free;
aReport.Free;
DBGrid.DataSource.DataSet.GotoBookmark(oldBmk);
DBGrid.DataSource.DataSet.FreeBookmark(oldBmk);
end;