最好是自己用TPRINTER控制打印.下例你修改一下即可
procedure TForm6.BitBtn3Click(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 //获取当前打印机的分辨率
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:=poPortrait;
//打印的字体和大小
printer.Canvas.Font.Name:='宋体';
printer.canvas.Font.Size:=12;
//根据字体的大小确定每行的高度
s:='出货统计明细表';
PrintStep:=printer.canvas.TextHeight(s)+16;
//打印的起点位置
x:=PointX*LeftBlank;
y:=PointY*TopBlank;
//DataSource1是DBGrid1所连接的数据源
if ((DM1.ds3.DataSet).Active=true) and ((DM1.ds3.DataSet).RecordCount>0) then
begin
printer.BeginDoc;
(dm1.ds3.DataSet).First;
while not (dm1.ds3.DataSet).Eof do
begin //打印DBGrid中的所有列
for i:=0 to DBGrid1.FieldCount-1 do
begin
//假如所要打印的列超出了打印范围,则忽略该列
if (x+DBGrid1.Columns.Items.Width*PointScale)<=(Printer.PageWidth-PointX*RightBlank) then
begin //画表格线
//每页的第一行打印表头
Printer.Canvas.Rectangle(x,y+200,x+DBGrid1.Columns.Items.Width*PointScale,y+200+PrintStep);
if y=PointY*TopBlank then begin
printer.Canvas.Font.Name:='黑体';
printer.canvas.Font.Size:=16;
Printer.Canvas.TextOut((Printer.PageWidth-printer.Canvas.TextWidth(s)) div 2,y+8,s);
printer.Canvas.Font.Name:='宋体';
printer.canvas.Font.Size:=12;
Printer.Canvas.TextOut(x+8,y+8+200,DBGrid1.Columns.Title.Caption);
end
else
Printer.Canvas.TextOut(x+8,y+200+8,DBGrid1.Fields.asString);
end; //计算下一列的横坐标
x:=x+DBGrid1.Columns.Items.Width*PointScale;
end;
if not (y=PointY*TopBlank) then
(dm1.ds3.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;