如何连续打印dbgrid中的数据(200分)

  • 主题发起人 主题发起人 虾子
  • 开始时间 开始时间

虾子

Unregistered / Unconfirmed
GUEST, unregistred user!
急用
明天晚上结贴
 
我用的是rave,delphi7
程序写到中间卡住了。
 
用ExLib中的DBGridEh,显示在Grid上的数据可以直接打印
另外有一个TPrintGrid控件也可用于直接打印Grid
自己编程实现简单功能(具体参见程序员编程指南-打印 ):
uses printer;
printline
printtitle
printdata
newpage
 
打印 TDBGrid内容
procedure PrintDbGrid(DataSet:TDataSet;DbGrid:TDbGrid;Title:String);
var
PointX,PointY:integer;
ScreenX:integer;
i,lx,ly:integer;
px1,py1,px2,py2:integer;
RowPerPage,RowPrinted:integer;
ScaleX:Real;
THeight:integer;
TitleWidth:integer;
SumWidth:integer;
PageCount:integer;
SpaceX,SpaceY:integer;
RowCount:integer;
begin
PointX:=Round(GetDeviceCaps(printer.Handle,LOGPIXELSX)/2.54);
PointY:=Round(GetDeviceCaps(printer.Handle,LOGPIXELSY)/2.54);
ScreenX:=Round(Screen.PixelsPerInch/2.54);
ScaleX:=PointX/ScreenX;
RowPrinted:=0;
SumWidth:=0;
printer.begin
Doc;
With Printer.Canvasdo
begin
DataSet.DisableControls;
DataSet.First ;
THeight:=Round(TextHeight('我')*1.5);//设定每行高度为字符高的1.5倍
SpaceY:= Round(TextHeight('我')/4);
SpaceX:=Round(TextWidth('我')/4);
RowPerpage:=Round((printer.PageHeight-5*PointY)/THeight);
//上下边缘各2厘米
ly:=2*PointY;
PageCount:=0;
while not DataSet.Eofdo
begin
if (RowPrinted=RowPerPage) or (RowPrinted=0) then
begin
if RowPrinted<>0 then
Printer.NewPage;
RowPrinted:=0;
PageCount:=PageCount+1;
Font.Name:='宋体';
Font.size:=16;
Font.Style:=Font.Style+[fsBold];
lx:=Round((Printer.PageWidth-TextWidth(Title))/2);
ly:=2*PointY;
TextOut(lx,ly,Title);
Font.Size:=11;
Font.Style:=Font.Style-[fsBold];
lx:=Printer.PageWidth-5*PointX;
ly:=Round(2*PointY+0.2*PointY);
if RowPerPage*PageCount>DataSet.RecordCount then
RowCount:=DataSet.RecordCount
else
RowCount:=RowPerPage*PageCount;
TextOut(lx,ly,'第'+IntToStr(RowPerPage*(PageCount-1)+1)+'-'+IntToStr(RowCount)+'条,共'+IntToStr(DataSet.RecordCount)+'条');
lx:=2*PointX;
ly:=ly+THeight*2;
py1:=ly-SpaceY;
if RowCount=DataSet.RecordCount then
py2:=py1+THeight*(RowCount-RowPerPage*(PageCount-1)+1)
else
py2:=py1+THeight*(RowPerPage+1);
SumWidth:=lx;
for i:=0 to DBGrid.Columns.Count-1do
begin
px1:=SumWidth-SpaceX;
px2:=SumWidth;
MoveTo(px1,py1);
LineTo(px2,py2);
TitleWidth:=TextWidth(DBGrid.Columns.Title.Caption);
lx:=Round(SumWidth+(DBGrid.Columns.width*scaleX-titleWidth)/2);
TextOut(lx,ly,DBGrid.Columns.Title.Caption);
SumWidth:=Round(SumWidth+DBGrid.Columns.width*scaleX)+SpaceX*2;
end;
px1:=SumWidth;
//画最后一条竖线
px2:=SumWidth;
MoveTo(px1,py1);
LineTo(px2,py2);
px1:=2*PointX;
//画第一条横线
px2:=SumWidth;
py1:=ly-SpaceY;
py2:=ly-SpaceY;
MoveTo(px1,py1);
LineTo(px2,py2);
py1:=py1+THeight;
py2:=py2+THeight;
MoveTo(px1,py1);
LineTo(px2,py2);
end;
lx:=2*PointX;
ly:=ly+THeight;
px1:=lx;
px2:=SumWidth;
py1:=ly-SpaceY+THeight;
py2:=ly-SpaceY+THeight;
MoveTo(px1,py1);
LineTo(px2,py2);
for i:=0 to DBGrid.Columns.Count-1do
begin
TextOut(lx,ly,DataSet.FieldByname(DBGrid.Columns.Fieldname).AsString);
lx:=Round(lx+DBGrid.Columns.width*ScaleX+SpaceX*2);
end;
RowPrinted:=RowPrinted+1;
DataSet.next;
end;
DataSet.first;
DataSet.EnableControls;
end;
printer.EndDoc;
end;

打印StringGrid内容
Procedure TACDListerMain.PrintTable;
Var
margins: TRect;
spacing: Integer;
Cols: TList;
Dlg: TPrintProgressDlg;
Procedure SetColumnWidth;
Var
i, k, w: Integer;
begin
Printer.Canvas.Font.Style := [ fsBold ];
For i := 0 To Pred( Grid.ColCount )do

Cols.Add( Pointer( Printer.Canvas.TextWidth( Grid.Cells[ i,0 ] )));
Printer.Canvas.Font.Style := [];
For i := 1 To Pred( Grid.RowCount )do
For k := 0 To Pred( Grid.ColCount )do
begin
w:= Printer.Canvas.TextWidth( Grid.Cells[ k, i ] );
If w > Integer( Cols[ k ] ) then
Cols[ k ] := Pointer( w );
end;
{ For }
w := 2 * Printer.Canvas.Font.PixelsPerInch div 3;
margins :=
Rect( w, w, Printer.PageWidth-w, Printer.PageHeight - w );
spacing := Printer.Canvas.Font.PixelsPerInch div 10;
w := 0;
For i := 0 To Pred(cols.Count)do
w := w + Integer( cols[ i ] ) + spacing;
w := w - spacing;
If w > (margins.right-margins.left ) then
begin
w := w - (margins.right-margins.left );
cols[ cols.Count-2 ] :=
Pointer( Integer( cols[ cols.Count-2 ] ) - w );
end;
{ If }
w:= 0;
For i := 0 To Pred(cols.Count)do
w := w + Integer( cols[ i ] ) + spacing;
margins.right := w - spacing + margins.left;
end;
{ SetColumnWidth }
Proceduredo
Print;
Var
i: Integer;
y: Integer;
Proceduredo
Line(lineno: Integer);
Var
x, n: Integer;
r: TRect;
th: Integer;
begin
If Length(Grid.Cells[0,lineno]) = 0 then
Exit;
x:= margins.left;
With Printer.Canvasdo
begin
th := TextHeight( '膟' );
For n := 0 To Pred( Cols.Count )do
begin
r := Rect( 0, 0, Integer(Cols[ n ]), th);
OffsetRect( r, x, y );
TextRect( r, x, y, Grid.Cells[ n, lineno ] );
x := r.right + spacing;
end;
{ For }
end;
{ With }
y := y + th;
end;
{do
Line }
Proceduredo
Header;
begin
y:= margins.top;
With Printer.Canvasdo
begin
Font.Style := [ fsBold ];
do
Line( 0 );
Pen.Width := Font.PixelsPerInch div 72;
Pen.Color := clBlack;
MoveTo( margins.left, y );
LineTo( margins.right, y );
Inc( y, 2 * Pen.Width );
Font.Style := [ ];
end;
{ With }
end;
{do
Header }
begin
y:= 0;
For i := 1 To Pred( Grid.RowCount )do
begin
Dlg.Progress( i );
Application.ProcessMessages;
If FPrintAborted then
Exit;
If y = 0 then
do
Header;
do
Line( i );
If y >= margins.bottom then
begin
Printer.NewPage;
y:= 0;
end;
{ If }
end;
{ For }
end;
{do
Print }
begin
FPrintAborted := False;
Dlg := TPrintProgressDlg.Create( Application );
With Dlgdo
try
OnAbort := PrintAborted;
Display( cPrintPreparation );
SetProgressRange( 0, Grid.RowCount );
Show;
Application.ProcessMessages;
Printer.Orientation := poLandscape;
Printer.begin
Doc;
Cols:= Nil;
try
Cols:= TLIst.Create;
Printer.Canvas.Font.Assign( Grid.Font );
SetColumnWidth;
Display( cPrintProceeding );
Application.ProcessMessages;
do
Print;
finally
Cols.Free;
If FPrintAborted then
Printer.Abort
else
Printer.EndDoc;
end;
finally
Close;
end;
{ With }
end;
{ TACDListerMain.PrintTable }
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2459631
 
谢谢楼上
不过你们的对我不实用
我写的程序是打印发票的
还有你上面的例子,我看不懂哈!
 
谢谢Siva,对我有用!
 
我的问题简化为下面的问题。
for i=0 to dbrid中的记录总数减1 do
a:=第i行中的第一列
b:=第i行中的第二列
.................
x:=第i行最后第一列
end;
写出这个这个循环就得分!
 
先来先得分
begin
x:=dbgrid1.datasource.dataset.recordcount;
for cnt:=0 to x-1do
begin

i:=dbgrid1.datasource.dataset.fieldbyname('x').asstring;
dbgrid1.datasource.dataset.edit;
dbgrid1.datasource.dataset.fieldvalues['flag']:='是';//打印标志设为是
dbgrid1.datasource.dataset.next;
end;

end;
我写的是一个变量,其他一样!

 
http://delphibbs.com/delphibbs/dispq.asp?lid=1746879
 
接受答案了.
 
后退
顶部