我使用的是RepotBuilder打印報表,DBGrid顯示數據,均採用動態生成數據源的方式:你可參考一下 生成DBGrid中的列頭: For i:=0 to DBGrid1.Columns.Count-1 do begin if i<2 then begin DBGrid1.Columns[0].Title.Caption:='料號'; DBGrid1.Columns[1].Title.Caption:='訂單號'; DBGrid1.Columns[i].Width:=90; end else begin DBGrid1.Columns[i].Width:=65; if (i mod 2)=0 then DBGrid1.Columns[i].Title.Caption:='段別' else DBGrid1.Columns[i].Title.Caption:='不良率'; end; end;生成打印報表: procedure TdfrmMrp_CodeStat.BitBtn2Click(Sender: TObject); var //打印 I,J:Integer; begin inherited; for I := 0 to ComponentCount - 1 do begin IF ( Components[I] is TppDBText ) then (Components[I] as TppDBText).DataField:=''; end; //清除數據連接 for J :=0 to DBGrid1.Columns.Count-2 do begin for I := 0 to ComponentCount - 1 do begin IF ( Components[I] is TppDBText ) then begin if (Components[I] as TppDBText).DataField='' then begin (Components[I] as TppDBText).DataField := DBGrid1.Columns[J+1].FieldName; Break; end; end; end; end; ppReport1.Print; end;
生成DBGrid中的列頭:
For i:=0 to DBGrid1.Columns.Count-1 do
begin
if i<2 then
begin
DBGrid1.Columns[0].Title.Caption:='料號';
DBGrid1.Columns[1].Title.Caption:='訂單號';
DBGrid1.Columns[i].Width:=90;
end
else
begin
DBGrid1.Columns[i].Width:=65;
if (i mod 2)=0 then DBGrid1.Columns[i].Title.Caption:='段別'
else DBGrid1.Columns[i].Title.Caption:='不良率';
end;
end;生成打印報表:
procedure TdfrmMrp_CodeStat.BitBtn2Click(Sender: TObject);
var //打印
I,J:Integer;
begin
inherited;
for I := 0 to ComponentCount - 1 do
begin
IF ( Components[I] is TppDBText ) then
(Components[I] as TppDBText).DataField:='';
end; //清除數據連接
for J :=0 to DBGrid1.Columns.Count-2 do
begin
for I := 0 to ComponentCount - 1 do
begin
IF ( Components[I] is TppDBText ) then
begin
if (Components[I] as TppDBText).DataField='' then
begin
(Components[I] as TppDBText).DataField := DBGrid1.Columns[J+1].FieldName;
Break;
end;
end;
end;
end;
ppReport1.Print;
end;