可以
具体代码如下:
procedure TForm1.PrintButtonClick(Sender: TObject);
const
LeftBlank=1;
RightBlank=1;
TopBlank=1;
BottomBlank=1;
var
PointX,PointY:integer;
PointScale,PrintStep:integer;
s:string;
x,y:integer;
i:integer;
begin
ComboBox1.Items.Assign(Printer.Printers);
if ComboBox1.Items.CommaText='' then
Messagedlg('Please install printer first!',mtError,[mbOk],0)
else begin
PointX:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/2.54);
PointY:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSY)/2.54);
PointScale:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/Screen.PixelsPerInch+0.5);
Printer.Orientation:=poLandscape;
Printer.Canvas.Font.Name:='宋体';
Printer.Canvas.Font.Size:=10;
s:=ADOTable1.TableName;
PrintStep:=Printer.Canvas.TextHeight(s)+16;
x:=PointX*LeftBlank;
y:=PointY*TopBlank;
if (DataSource1.DataSet.Active=true)and(DataSource1.DataSet.RecordCount>0) then
begin
printer.BeginDoc;
DataSource1.DataSet.First;
while not DataSource1.DataSet.Eof do
begin
for i:=0 to DBGrid1.FieldCount-1 do
begin
if (x+DBGrid1.Columns.Items[i].Width*PointScale)<=(Printer.PageWidth-PointX*RightBlank) then
begin
Printer.Canvas.Rectangle(x,y,x+DBGrid1.Columns.Items[i].Width*PointScale,y+PrintStep);
if y=PointY*TopBlank then
Printer.Canvas.TextOut(x+8,y+8,DBGrid1.Columns[i].Title.Caption)
else
Printer.Canvas.TextOut(x+8,y+8,DBGrid1.Fields[i].AsString);
end;
x:=x+DBGrid1.Columns.Items[i].Width*PointScale;
end;
if not (y=PointY*TopBlank) then
DataSource1.DataSet.Next;
x:=PointX*LeftBlank;
y:=y+PrintStep;
if (y+PrintStep)>(Printer.PageHeight-PointY*BottomBlank) then
begin
Printer.NewPage;
y:=PointY*TopBlank;
end;
end;
Printer.EndDoc;
DataSource1.DataSet.First;
Application.MessageBox('Print complete!','Print',32);
end;
end;
end;
具体代码如下:
procedure TForm1.PrintButtonClick(Sender: TObject);
const
LeftBlank=1;
RightBlank=1;
TopBlank=1;
BottomBlank=1;
var
PointX,PointY:integer;
PointScale,PrintStep:integer;
s:string;
x,y:integer;
i:integer;
begin
ComboBox1.Items.Assign(Printer.Printers);
if ComboBox1.Items.CommaText='' then
Messagedlg('Please install printer first!',mtError,[mbOk],0)
else begin
PointX:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/2.54);
PointY:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSY)/2.54);
PointScale:=Trunc(GetDeviceCaps(Printer.Handle,LOGPIXELSX)/Screen.PixelsPerInch+0.5);
Printer.Orientation:=poLandscape;
Printer.Canvas.Font.Name:='宋体';
Printer.Canvas.Font.Size:=10;
s:=ADOTable1.TableName;
PrintStep:=Printer.Canvas.TextHeight(s)+16;
x:=PointX*LeftBlank;
y:=PointY*TopBlank;
if (DataSource1.DataSet.Active=true)and(DataSource1.DataSet.RecordCount>0) then
begin
printer.BeginDoc;
DataSource1.DataSet.First;
while not DataSource1.DataSet.Eof do
begin
for i:=0 to DBGrid1.FieldCount-1 do
begin
if (x+DBGrid1.Columns.Items[i].Width*PointScale)<=(Printer.PageWidth-PointX*RightBlank) then
begin
Printer.Canvas.Rectangle(x,y,x+DBGrid1.Columns.Items[i].Width*PointScale,y+PrintStep);
if y=PointY*TopBlank then
Printer.Canvas.TextOut(x+8,y+8,DBGrid1.Columns[i].Title.Caption)
else
Printer.Canvas.TextOut(x+8,y+8,DBGrid1.Fields[i].AsString);
end;
x:=x+DBGrid1.Columns.Items[i].Width*PointScale;
end;
if not (y=PointY*TopBlank) then
DataSource1.DataSet.Next;
x:=PointX*LeftBlank;
y:=y+PrintStep;
if (y+PrintStep)>(Printer.PageHeight-PointY*BottomBlank) then
begin
Printer.NewPage;
y:=PointY*TopBlank;
end;
end;
Printer.EndDoc;
DataSource1.DataSet.First;
Application.MessageBox('Print complete!','Print',32);
end;
end;
end;
还可以将grid中的内容导入到excel里面,也可以打印
还是用QuickReport!