可以不用第三方控件,用F1BOOK可以实现,具体方法:
procedure FillBookWithDBGrid(F1Book:TF1Book;DBGrid:TDBGrid;CXReportTitle,CXCondition:string);
Var
RecordCount : integer;
I:integer;
begin
DBGrid.DataSource.DataSet.DisableControls;
RecordCount := DBGrid.DataSource.DataSet.RecordCount;
with F1Bookdo
begin
MaxCol:=DBGrid.Columns.Count;
MaxRow := RecordCount+3;
SetSelection(1,1,1,MaxCol);//Report's Title;
Text := CxReportTitle;
SetAlignment(F1HAlignCenterAcrossCells,FALSE,F1VAlignCenter,0);
SetFont('¿¬Ìå_GB2312',16,TRUE,FALSE,FALSE,FALSE,clBlack,FALSE,FALSE);
SetSelection(2,1,MaxRow,MaxCol);
SetAlignment(F1HAlignLeft,FALSE,F1VAlignCenter,0);
SetFont('ËÎÌå',12,FALSE,FALSE,FALSE,FALSE,clBlack,FALSE,FALSE);
Text := CxCondition;
for I:=1 to MaxColdo
begin
SetSelection(3,I,3,I);
Text := DBGrid.Columns[I-1].Title.Caption;
end;
DBGrid.DataSource.DataSet.DisableControls;
FillCellsWithTable(F1Book,DBGrid.DataSource.DataSet,0,3);
DBGrid.DataSource.DataSet.EnableControls;
//Set format for print.
SetSelection(3,1,3,MaxCol);
SetAlignment(F1HAlignCenter,FALSE,F1VAlignCenter,0);//columnheader. hcenter
SetColWidthAuto(3,1,MaxRow,MaxCol,TRUE);//autoresize col;
if MaxCol>=3 then
begin
SetSelection(4,3,MaxRow,MaxCol);
SetAlignment(F1HAlignleft,FALSE,F1VAlignCenter,0);//columnheader. hcenter
end;
//Border
SetSelection(3,1,MaxRow,MaxCol);
SetBorder( 1, 1, 1, 1, 1, 1, 1, rgb(0,0,0), rgb(0,0,0), rgb(0,0,0), rgb(0,0,0));
end;
DBGrid.DataSource.DataSet.EnableControls;
end;
procedure PrintF1book(F1book: TObject);
begin
with (F1book as TF1book)do
begin
try
printfooter:='';
printgridlines:=false;
printheader:='';
printnocolor:=true;
SetSelection(1,1,1,1);
// SetPrintScale(400, true, 1, 1);
printArea:='a1:'+chr(ord('a')+Maxcol-1)+inttostr(MaxRow);
fileprint(true);
except
On EOleExceptiondo
Application.MessageBox('´òÓ¡×÷Òµ±»Óû§È¡Ïû
......','µÇ¼Ç²éѯ',0);
end;
end;
end;