自己写的:
unit ListPrint;
interface
uses printers,Types,ComCtrls,Classes,Graphics,SysUtils;
type
TmyCol = record
text : string[30];
width : integer;
iType : byte;
// 1左 2 中 3 右
end;
procedure PrintRect(R : TRect;
txt: string;
iType:integer );//打印单元
procedure PagPrint1(ListV:TlistView;
sTitle1,sTitle2,sTitle3: string);
//打印TlistView类型的
procedure PagPrint(Cols: array of TmyCol;
Dat: array of pchar;
sTitle1,sTitle2,sTitle3: string;
colCount,RowCount:integer);
//通用的
implementation
procedure PrintRect(R : TRect;
txt: string;
iType:integer );//打印单元
var
L,dx : integer;
begin
l := printer.Canvas.TextWidth(txt);
//内容长度
if l>(R.Right -R.Left) then
dx:=20
else
case iType of
1 : dx := 20 ;//左
2 : dx := trunc(((R.Right -R.Left)-L)/2);
3 : dx := (R.Right -R.Left)-L-20;
else
dx := 20;
end;
// printer.Canvas.Rectangle(R);
printer.Canvas.TextRect(R,R.Left+dx,R.Top+20,txt);
end;
procedure PagPrint1(ListV:TlistView;
sTitle1,sTitle2,sTitle3: string);
var
i,j : integer;
Rowcount ,ColCount : integer;
cols :array of TmyCol;
sText: string;
dat : array of Pchar;
begin
RowCount := ListV.Items.Count;
ColCount := ListV.Columns.Count;
setlength(Cols,Colcount);
setLength(dat,RowCount* Colcount);
for i := 0 to Colcount-1do
begin
Cols.text := ListV.Columns.Caption;
cols.width :=ListV.Columns.Width;
case ListV.Columns.Alignment of
taLeftJustify : cols.iType:= 1;
taCenter : cols.iType:= 2;
else
cols.iType:= 3
end;
end;
for i := 0 to RowCount-1do
begin
for j := 0 to Colcount-1do
begin
sText := '';
if j=0 then
sText := ListV.Items.Caption
else
try
sText := ListV.Items.SubItems[j-1];
except
sText:= '';
end;
dat[i*ColCount+j]:= pchar(sText);
end;
end;
PagPrint(cols,dat,sTitle1,sTitle2,sTitle3,colCount,RowCount);
end;
{
RowCount : integer;
//总行数
colCount : integer;
//列的个数
}
procedure PagPrint(Cols: array of TmyCol;
Dat: array of pchar;
sTitle1,sTitle2,sTitle3: string;
colCount,RowCount:integer);
var
R : TRect;
i,n,L : integer;
sTxt : string;
RowIndex : integer;
//当前行号
pagIndex : integer;
PagH : integer;
//有效页高
dWidth :integer;
//显示与打印的比例系数
y0 : integer;
x,y : integer;
PagR : TRect;
//有效打印区域
begin
if RowCount=0 then
exit;
// printer.Orientation:=poLandscape ;
PagR.Left:= 400;
pagR.Top:= 200;
pagR.Right:= printer.PageWidth-400;
pagR.Bottom:= Printer.PageHeight-400;
PagH := pagR.Bottom-100 ;
printer.Canvas.Brush.Style:= bsClear;
//取列信息
printer.begin
Doc;
L := 0;
for i := 0 to ColCount-1do
l:=l+cols.width;
dWidth := trunc((pagR.Right-pagR.Left)/ l);
RowIndex := 0;
pagIndex :=1;
while RowCount> RowIndexdo
begin
//打印标题
sTxt := sTitle1;
printer.Canvas.Font.Size:=-22;
Printer.Canvas.Font.Name:='黑体';
R.TopLeft:=point(pagR.Left, pagR.Top);
R.Right:= pagR.Right;
R.Bottom:=R.Top+ printer.Canvas.TextHeight('ABC')+40;
PrintRect(R, sTxt,2 );
//居中
Printer.Canvas.Font.Name:='宋体';
//打印副标题
sTxt := sTitle2;
printer.Canvas.Font.Size:=-11;
R.TopLeft:= point(R.Left,650);
R.Right:= pagR.Right;
R.Bottom := R.Top+ printer.Canvas.TextHeight('ABC')+40;
PrintRect(R, sTxt,1 );
//居左
sTxt := '制表日期:'+formatdatetime('yyyy-mm-dd hh:n:ss ',now);
PrintRect(R, sTxt,3 );
//居右
//打印脚注
sTxt := sTitle3;
printer.Canvas.Font.Size:=-9;
R.TopLeft:=point(pagR.Left, pagR.Bottom);
R.BottomRight:=point(pagR.Right, pagR.Bottom+printer.Canvas.TextHeight('ABC')+40);
PrintRect(R, sTxt,1 );
//居左
//打印脚注2
sTxt := format('第 %d 页',[pagIndex]);
PrintRect(R, sTxt,2 );
//居中
//打印脚注3
// sTxt := '制表日期:'+formatdatetime('yyyy-mm-dd hh:n:ss ',now);
// PrintRect(R, sTxt,3 );
//居右
printer.Canvas.Font.Size:=-9;
// 1 打印表格头
y0:= 800;
R.Top := y0;
R.Bottom:= R.Top + trunc(printer.Canvas.TextHeight('ABC')*1.5);
L := pagR.Left;
Printer.Canvas.Font.Name:='黑体';
for i := 0 to ColCount-1do
begin
R.Left:= L;
R.Right:= trunc(R.Left+Cols.width*dWidth);
PrintRect(R, cols.text, cols.iType);
L:= R.Right;
end;
Printer.Canvas.Font.Name:='宋体';
//打印数据行
while R.Bottom< PagH do
begin
if RowIndex >= RowCount then
break;
R.Top:=R.Bottom;
R.Bottom:= R.Top + trunc(printer.Canvas.TextHeight('ABC')*1.5);
L := pagR.Left;
for i := 0 to ColCount-1do
begin
R.Left:= L;
R.Right:= trunc(R.Left+Cols.width*dWidth);
n := RowIndex*(ColCount)+i;
//取数据
sTxt := dat[n];
PrintRect(R, sTxt, cols.iType);
L:= R.Right;
end;
//横线
printer.Canvas.MoveTo(pagR.Left, R.Top);
printer.Canvas.LineTo(R.Right, R.Top);
inc(RowIndex);
end;
//网格
printer.Canvas.Pen.Width:= 2;
printer.Canvas.Rectangle(pagR.Left,y0,R.Right+1,R.Bottom+1);
printer.Canvas.Pen.Width:= 2;
L:= pagR.Left;
y:= y0;
for i := 0 to ColCount-2do
begin
x:= L+ trunc(cols.width*dWidth);
L := x;
printer.Canvas.MoveTo(x,y);
printer.Canvas.LineTo(x,R.Bottom);
end;
inc(pagIndex);
if RowIndex < RowCount then
printer.NewPage;
end;
printer.EndDoc;
end;
end.