procedure Tmainprintform.PrintDBGrid(DBGrid: TDBGrid;
bh: string;xq1:string;xq2:string);
var
i, k,aLeft: integer;
xs:real;
//系数,用于计算DBGrid各列所需的宽度(DBGrid列的度*系数)
begin
K:=-1;
if not DBGrid.DataSource.DataSet.Active then
begin
ShowMessage('数据集没有打开!');
Exit;
end;
DBGrid.DataSource.DataSet.First;
mainprintform.DataSet := DBGrid.DataSource.DataSet;
QRLabel3.Caption:=bh;
//打印系数bh的内容;
QRLabel4.Caption:=xq1;
//打印系数xq1的内容;
QRLabel5.Caption:=xq2;
//打印系数xq2的内容
//////选择纸张
aLeft := 0;
for i := 0 to DBGrid.FieldCount - 1do
if DBGrid.Columns.Width>=20 then
aLeft := aLeft + DBGrid.Columns.Width ;
// else
// DBGrid.Columns.Visible:=false;
//可让DBGrid.Columns不显示
if aLeft >741 then
begin
mainprintform.Page.Orientation:= poLandscape;
xs:=1070/aleft ;
QRSysData2.Left:=925;
QRLabel7.Left:=990;
end
else
if (aleft>0) and (aleft<=741) then
begin
mainprintform.Page.Orientation := poPortrait;
xs:=741/aleft;
QRSysData2.Left:=615;
QRLabel7.Left:=670;
end
else
begin
xs:=1;
application.MessageBox('没什么要打印的吗??','提示......');
end;
////
aLeft := 0;
for i := 0 to DBGrid.FieldCount - 1do
begin
if (DBGrid.Columns.Visible = false) or (DBGrid.Columns.Width<15) then
//当DBGrid.Columns.不可见、宽度小于15时不打印
continue;
K:=k+1;
//打印标题的框格
with TQRShape(QRBand1.AddPrintable(TQRShape))do
begin
Height :=25;
Top :=115;
Left := aLeft-k;
Width := round(DBGrid.Columns.Width*xs);
end;
//打印标题
with TQRLabel(QRBand1.AddPrintable(TQRLabel))do
begin
AutoSize := false;
Width := round(DBGrid.Columns.Width*xs)-4;
Caption := DBGrid.Columns.Title.Caption;
Alignment := taCenter;
Top := 122;
Left := aLeft-k+2;
end;
//DBGrid的框格
with TQRDBText(QRBand2.AddPrintable(TQRShape))do
begin
Top := 0;
Left := aLeft-k;
height:=25;
Width :=round(DBGrid.Columns.Width*xs);
end;
//DBGrid的内容
with TQRDBText(QRBand2.AddPrintable(TQRDBText))do
begin
DataSet := DBGrid.DataSource.DataSet;
DataField := DBGrid.Columns.FieldName;
Alignment := DBGrid.Columns.Alignment;
Top := 6;
Left := aLeft-k+2;
AutoSize := False;
Width := round(DBGrid.Columns.Width*xs)-4;
height:=14;
end;
aLeft := aLeft + round(DBGrid.Columns.Width*xs) ;
end;
//最后一条线,
with TQRDBText(QRBand3.AddPrintable(TQRShape))do
begin
Top := 0;
Left := 0;
height:=1;
Width :=ALEFT-K;
end;
end;
把str1:=打印条件,
str2, str3:=其他打印内容
A4的纸张+自动大小