动态创建Fastreport分以下几个步骤:
1.首先清空Fastreport,定义全局变量,并加载数据集
frReport.Clear;
frReport.DataSets.Add(frxDBDataset1);
DataHeight :=28;
DataWidth :=80;
FirstTop := 50;
FirstLeft := 15;
2.创建frxReportPage,并设置纸张大小
Page := TfrxReportPage.Create(frReport);
Page.CreateUniqueName;
page.Orientation := poLandscape;
Page.SetDefaults; //默认大小
//Page.PaperWidth := 210;
//Page.PaperHeight := 297;
//以下为动态设置纸张大小
{if (qryPrn.FieldCount-4)<=6 then //当列小于6
begin
Page.PaperWidth := 210;
Page.PaperHeight := 279.4;
page.Orientation := poPortrait;
end
else
begin
page.Orientation := poLandscape;
iWidth := 40+85+90+(qryPrn.FieldCount-4)*84+30;
//fr01cm=3.77953;
if iWidth<297*fr01cm then
//fr01cm为frxClass中定义的常量,为每毫米像素值,或PerMillPix := 25.38/Screen.PixelsPerInch;(25.38为每英寸毫米数)
begin
Page.PaperSize :=DMPAPER_A4; //设置为A4,因为激光打印机不支持自定义纸张的横打
end
else
begin
Page.PaperWidth :=iWidth/fr01cm;
Page.PaperHeight := 210;
end;
end;}
3.生成PageBand,并指定高度
Band := TfrxPageHeader.Create(Page); //页首
Band.Top := 10;
Band.Height := 150;
BandFoot := TfrxColumnFooter.Create(Page); //页脚
BandFoot.Top := 10;
BandFoot.Height := 50;
4.画页脚栏
//画操作员
memFoot := TfrxMemoView.Create(BandFoot);
with memFoot do
begin
CreateUniqueName;
HAlign := haLeft;
VAlign := vaCenter;
Memo.Text := '操作员:程序员';
SetBounds(40, 10, 150, 20);
ParentFont := False;
Font.Name := '宋体';
Font.Size := 10;
end;
//画打印时间
memFoot := TfrxMemoView.Create(BandFoot);
with memFoot do
begin
CreateUniqueName;
Align := baLeft;
HAlign := haLeft;
VAlign := vaCenter;
Memo.Text := '打印时间:' + FormatDateTime('yyyy-mm-dd hh:mm:ss', Now);
SetBounds(280, 10, 270, 20);
ParentFont := False;
Font.Name := '宋体';
Font.Size := 11;
end;
5.画标题栏
//画标题
Memo := TfrxMemoView.Create(Band);
with Memo do
begin
CreateUniqueName;
Align := baCenter;
Memo.Text := '******余额汇总表';
Frame.Typ :=[ftBottom];
Frame.BottomLine.Style :=fsDouble; //行类型
Top := FirstTop;
Height := 30;
AutoWidth :=True;
HAlign := haCenter;
ParentFont := False;
Font.Name := '黑体';
Font.Size := 16;
end;
//画单位
Memo := TfrxMemoView.Create(Band);
with Memo do
begin
CreateUniqueName;
HAlign := haLeft;
VAlign := vaCenter;
Memo.Text := '单位:************';
FirstTop := FirstTop + 40;
FirstWidth := 200;
FirstHeight := 30;
SetBounds(40, FirstTop, FirstWidth, FirstHeight);
ParentFont := False;
Font.Name := '宋体';
Font.Size := 10;
end;
//画日期
Memo := TfrxMemoView.Create(Band);
with Memo do
begin
CreateUniqueName;
Align := baLeft;
VAlign := vaCenter;
HAlign := haLeft;
Memo.Text := '日期:' + FormatDateTime('yyyy-mm-dd', Date);
FirstWidth := 10 + FirstWidth + 200;
FirstLeft := FirstLeft + FirstWidth;
SetBounds(FirstLeft, FirstTop, 200, FirstHeight);
ParentFont := False;
Font.Name := '宋体';
Font.Size := 10;
end;
//画页码
Memo := TfrxMemoView.Create(Band);
with Memo do
begin
CreateUniqueName;
Align := baLeft;
HAlign := haLeft;
VAlign := vaCenter;
Memo.Text := '第[Page#]/[TotalPages#]页'; //[]中括起来的是FastReport系统变量
FirstWidth := 10 + FirstWidth;
FirstLeft := FirstLeft + 200;
SetBounds(FirstLeft, FirstTop, 100, FirstHeight);
HAlign := haCenter;
ParentFont := False;
Font.Name := '宋体';
Font.Size := 10;
end;
for i := 1 to qryPrn.FieldCount - 4 do
begin
//画数据集的标题
Memo := TfrxMemoView.Create(Band);
Memo.CreateUniqueName;
Memo.ParentFont := False;
Memo.Font.Name := '宋体';
Memo.Font.Size := 10;
Memo.Text := qryPrn.Fields[i + 3].FieldName;
Memo.Frame.Typ := [ftTop, ftBottom, ftRight];
Memo.SetBounds(220 + (i - 1) * DataWidth, Band.Height - DataHeight, DataWidth, DataHeight);
Memo.HAlign := haCenter;
Memo.VAlign := vaCenter;
end;
6.画数据集
DataBand := TfrxMasterData.Create(Page);
DataBand.CreateUniqueName;
DataBand.DataSet := frxDBDataset1;
DataBand.Top := 200;
DataBand.Height := DataHeight;
Memo1 := TfrxMemoView.Create(DataBand);
with Memo1 do
begin
CreateUniqueName;
ParentFont := False;
SetBounds(30, 0, 90, DataHeight);
VAlign := vaCenter;
HAlign := haLeft;
Font.Name := '宋体';
Font.Size := 10;
DataSet := frxDBDataset1;
DataField := 'locus';
Frame.Typ := [ftRight, ftBottom, ftLeft];
end;
Memo1 := TfrxMemoView.Create(DataBand);
with Memo1 do
begin
CreateUniqueName;
ParentFont := False;
SetBounds(120, 0, 100, DataHeight);
VAlign := vaCenter;
HAlign := haRight;
Font.Name := '宋体';
Font.Size := 10;
DataSet := frxDBDataset1;
DataField := 'TotalMoney';
Frame.Typ := [ftRight, ftBottom]; //加上右边和底部的边框
end;
for i := 1 to qryPrn.FieldCount - 4 do
begin
Memo1 := TfrxMemoView.Create(DataBand);
Memo1.CreateUniqueName;
Memo1.ParentFont := False;
Memo1.SetBounds(220 + (i - 1) * DataWidth, 0, DataWidth, DataHeight);
Memo1.DisplayFormat.DecimalSeparator := '.';
//Memo1.ExpressionDelimiters := ',';
//Memo1.DisplayFormat.FormatStr := '%2.2f'; 定义显示格式
//Memo1.DisplayFormat.Kind := fkNumeric;
Memo1.VAlign := vaCenter;
Memo1.HAlign := haRight;
Memo1.Font.Name := '宋体';
Memo1.Font.Size := 10;
Memo1.DataSet := frxDBDataset1;
Memo1.DataField := qryPrn.Fields[i + 3].FieldName;
Memo1.Frame.Typ := [ftRight, ftBottom];
end;
7.显示
frReport.PrepareReport();
frReport.ShowReport();
8.将以上代码用函数表示
(1)定义
TBandType=(PageHeader,PageFooter);
procedure CreatePage(var Page:TfrxReportPage;frxReport:TfrxReport;iWidth,iHeight:Double;pDirect:TPrinterOrientation);
procedure CreateBand(var band:TfrxBand;Page:TfrxReportPage;iTop,iHeight:Double;BandType:TBandType);
procedure CreateDataBand(var band:TfrxMasterData;Page:TfrxReportPage;DbSet:TfrxDBDataset;iTop,iHeight:Integer);
procedure CreateMemoData(var memView: TfrxMemoView; Band: TfrxBand;
iLeft, iTop, iWidth, iHeight: Extended; fontname: string; fontsize: Integer;
Halign: TfrxHAlign; Valign: TfrxVAlign; frxData: TfrxDBDataset; DField: string;
FrameType: TfrxFrameTypes; Delimiters, ForStr: string; fkKind: TfrxFormatKind);
procedure CreateMemo(var memView: TfrxMemoView; Band: TfrxBand;
iLeft, iTop, iWidth, iHeight: Extended; fontname: string; fontsize: Integer;
Halign: TfrxHAlign; Valign: TfrxVAlign; FrameType: TfrxFrameTypes; sText: string);
(2)实现
procedure TForm1.CreatePage(var Page: TfrxReportPage;
frxReport: TfrxReport; iWidth, iHeight: Double;
pDirect: TPrinterOrientation);
begin
Page := TfrxReportPage.Create(frxReport);
Page.CreateUniqueName;
Page.PaperWidth :=iWidth;
Page.Height :=iHeight;
Page.Orientation :=pDirect;
end;
procedure TForm1.CreateBand(var band: TfrxBand; Page: TfrxReportPage; iTop,
iHeight:Double;BandType:TBandType);
begin
case BandType of
PageHeader: band :=TfrxPageHeader.Create(Page);
PageFooter: band :=TfrxPageFooter.Create(Page);
end;
band.CreateUniqueName;
band.Top :=iTop;
band.Height :=iHeight;
end;
procedure TForm1.CreateDataBand(var band: TfrxMasterData;
Page: TfrxReportPage; DbSet: TfrxDBDataset; iTop, iHeight: Integer);
begin
band :=TfrxMasterData.Create(Page);
band.CreateUniqueName;
band.DataSet := DbSet;
band.Top :=iTop;
band.Height :=iHeight;
end;
procedure TForm1.CreateMemo(var memView: TfrxMemoView; Band: TfrxBand;
iLeft, iTop, iWidth, iHeight: Extended; fontname: string;
fontsize: Integer; Halign: TfrxHAlign; Valign: TfrxVAlign;
FrameType: TfrxFrameTypes; sText: string);
begin
memView := TfrxMemoView.Create(Band);
memView.CreateUniqueName;
memView.ParentFont := False;
memView.Font.Name := fontname;
memView.Font.Size := fontsize;
memView.SetBounds(iLeft, iTop, iWidth, iHeight);
memView.HAlign := Halign;
memView.VAlign := Valign;
memView.Frame.Typ := FrameType;
memView.Memo.Text := sText;
end;
procedure TForm1.CreateMemoData(var memView: TfrxMemoView; Band: TfrxBand;
iLeft, iTop, iWidth, iHeight: Extended; fontname: string;
fontsize: Integer; Halign: TfrxHAlign; Valign: TfrxVAlign;
frxData: TfrxDBDataset; DField: string; FrameType: TfrxFrameTypes;
Delimiters, ForStr: string; fkKind: TfrxFormatKind);
begin
memView := TfrxMemoView.Create(Band);
memView.CreateUniqueName;
memView.ParentFont := False;
memView.Font.Name := fontname;
memView.Font.Size := fontsize;
memView.SetBounds(iLeft, iTop, iWidth, iHeight);
memView.HAlign := Halign;
memView.VAlign := Valign;
memView.DataSet := frxData;
memView.DataField := DField;
memView.Frame.Typ := FrameType;
memView.ExpressionDelimiters := Delimiters;
memView.DisplayFormat.FormatStr := ForStr;
memView.DisplayFormat.Kind := fkKind;
end;
(3)引用代码
const
arrFields:array[1..12,1..2] of string=(('Quantity_Start','数量'),('Expense_Start','金额'),
('Quantity_Buy_Add','数量'),('Quantity_Buy_Add','金额'),
('Quantity_Other_Add','数量'),('Expense_Other_Add','金额'),
('Quantity_Out_Reduce','数量'),('Expense_Out_Reduce','金额'),
('Quantity_Other_Reduce','数量'),('Expense_Other_Reduce','金额'),
('Quantity_End','数量'),('Expense_End','金额')
);
var
FirstTop, FirstWidth, FirstHeight, i, DataWidth, DataHeight, MlWidth: Integer;
Page: TfrxReportPage;
BandHeader, BandFoot: TfrxBand;
DataBand: TfrxMasterData;
Memo, Memo1, memFoot: TfrxMemoView;
iLeft, iWidth, iLeft1: Integer;
PerMillPix: Double;
iFont: Integer;
haAl: TfrxHAlign;
MemArray: array[0..4] of TfrxMemoView;
MemArray1, MemDataArray: array[0..12] of TfrxMemoView;
MemArray2, MemArray3: array[0..3] of TfrxMemoView;
frx:TfrxComponent;
begin
frReport.Clear;
iFont := iFont10;
frxDBDataset1.DataSet := qryPrn;
frReport.DataSets.Add(frxDBDataset1);
CreatePage(Page, frReport, 210, 279.4, poLandscape);
//fr01cm :=3.77953;
DataHeight := 25; //DataHeight*3为栏首高度(目录高度)
MlWidth := 84; //目录宽度
DataWidth := 40;
FirstTop := 40;
FirstHeight := 30;
iLeft := 60;
iLeft1 := 40;
CreateBand(BandHeader, Page, 10, 220, PageHeader);
CreateBand(BandFoot, Page, 10, 50, PageFooter);
//画操作员
CreateMemo(memFoot, BandFoot, 65, 10, 150, 20, FName, iFont, haLeft, vaCenter,
[], '操作员:'+IGlobalVar.GetUserName);
//画打印时间
CreateMemo(memFoot, BandFoot, 280, 10, 270, 20, FName, iFont, haLeft, vaCenter,
[], '打印时间:' + FormatDateTime('yyyy-mm-dd hh:mm:ss', Now));
//画标题
Memo := TfrxMemoView.Create(BandHeader);
with Memo do
begin
CreateUniqueName;
Align := baCenter;
Memo.Text := '**********汇总表';
Frame.Typ := [ftBottom];
Frame.BottomLine.Style := fsDouble;
Top := FirstTop;
Height := 24;
AutoWidth := True;
HAlign := haCenter;
ParentFont := False;
Font.Name := '黑体';
Font.Size := 16;
end;
//画单位
FirstTop := FirstTop + 40;
FirstWidth := 150;
CreateMemo(MemArray[0], BandHeader, 65, FirstTop, FirstWidth, FirstHeight, FName, iFont, haLeft,
vaCenter,
[], '单位:'+IGlobalVar.GetUnitID);
//画日期
CreateMemo(MemArray[1], BandHeader, 128, MemArray[0].Top,
270, FirstHeight, FName, iFont, haLeft, vaCenter,
[], '日期:' + FormatDateTime('yyyy年mm月dd日', Date) + '至' +
FormatDateTime('yyyy年mm月dd日', Date));
//画仓库
FirstWidth := 170;
CreateMemo(MemArray[2], BandHeader, MemArray[0].Left, MemArray[0].Top + 20, FirstWidth,
FirstHeight, FName, iFont, haLeft, vaCenter,
[], '仓库:'+LcbWh.Text);
//画分类方法
FirstWidth := 10 + FirstWidth;
CreateMemo(MemArray[3], BandHeader, MemArray[1].Left, MemArray[2].Top, 200, FirstHeight, '宋体',
iFont, haLeft, vaCenter,
[], '分类方法:'+lcb1.Text);
//画页码
FirstWidth := 10 + FirstWidth;
CreateMemo(MemArray[4], BandHeader, MemArray[3].Left + MemArray[3].Width + 100, MemArray[2].Top,
100, FirstHeight, FName, iFont, haLeft, vaCenter,
[], '第[Page#]页 共[TotalPages#]页');
//画栏标题
iWidth := 32 * Pix10; //14个汉字(28字符)所用的像素值
CreateMemo(MemArray1[0], BandHeader, iLeft, BandHeader.Height - DataHeight * 3, iWidth, DataHeight
* 3, FName, iFont,
haCenter, vaCenter, [ftTop, ftRight, ftBottom, ftLeft], '目录');
iLeft := iLeft + iWidth;
for i := 1 to 12 do
begin
if (i = 1) or (i = 2) or (i = 11) or (i = 12) then
CreateMemo(MemArray1[i], BandHeader, MemArray1[i - 1].Left + MemArray1[i - 1].Width,
BandHeader.Height -
DataHeight * 2, GetWidth(i),
DataHeight * 2,
FName, iFont, haCenter, vaCenter, [ftTop, ftBottom, ftRight], arrFields[i, 2])
else
CreateMemo(MemArray1[i], BandHeader, MemArray1[i - 1].Left + MemArray1[i - 1].Width,
BandHeader.Height -
DataHeight, GetWidth(i),
DataHeight,
FName, iFont, haCenter, vaCenter, [ftTop, ftBottom, ftRight], arrFields[i, 2]);
end;
CreateMemo(Memo, BandHeader, MemArray1[3].Left,MemArray1[1].Top,
MemArray1[3].Width+MemArray1[4].Width,DataHeight,
FName, iFont, haCenter, vaCenter, [ftTop, ftBottom, ftRight], '外购');
CreateMemo(Memo, BandHeader, MemArray1[5].Left,MemArray1[1].Top,
MemArray1[5].Width+MemArray1[6].Width,DataHeight,
FName, iFont, haCenter, vaCenter, [ftTop, ftBottom, ftRight], '其它');
CreateMemo(Memo, BandHeader, MemArray1[7].Left,MemArray1[1].Top,
MemArray1[7].Width+MemArray1[8].Width,DataHeight,
FName, iFont, haCenter, vaCenter, [ftTop, ftBottom, ftRight], '报废');
CreateMemo(Memo, BandHeader, MemArray1[9].Left,MemArray1[1].Top,
MemArray1[9].Width+MemArray1[10].Width,DataHeight,
FName, iFont, haCenter, vaCenter, [ftTop, ftBottom, ftRight], '其它');
CreateMemo(Memo, BandHeader, MemArray1[1].Left,MemArray1[0].Top,
MemArray1[1].Width+MemArray1[2].Width,DataHeight,
FName, iFont, haCenter, vaCenter, [ftTop, ftBottom, ftRight], '期初余额');
CreateMemo(Memo, BandHeader, MemArray1[3].Left,MemArray1[0].Top,
MemArray1[3].Width+MemArray1[4].Width+MemArray1[5].Width+MemArray1[6].Width,DataHeight,
FName, iFont, haCenter, vaCenter, [ftTop, ftBottom, ftRight], '本期增加');
CreateMemo(Memo, BandHeader, MemArray1[7].Left,MemArray1[0].Top,
MemArray1[7].Width+MemArray1[8].Width+MemArray1[9].Width+MemArray1[10].Width,DataHeight,
FName, iFont, haCenter, vaCenter, [ftTop, ftBottom, ftRight], '本期减少');
CreateMemo(Memo, BandHeader, MemArray1[11].Left,MemArray1[0].Top,
MemArray1[11].Width+MemArray1[12].Width,DataHeight,
FName, iFont, haCenter, vaCenter, [ftTop, ftBottom, ftRight], '期末余额');
CreateDataBand(DataBand, Page, frxDBDataset1, 200, DataHeight);
iWidth := 85;
CreateMemoData(MemDataArray[0], DataBand, MemArray1[0].Left, 0, MemArray1[0].Width, DataHeight,
FName, iFont, haLeft, vaCenter,
frxDBDataset1, 'contents', [ftRight, ftBottom, ftLeft], '', '', fkText);
frReport.Script.Clear;
frReport.ScriptLanguage :='PascalScript';
frReport.ScriptText.LoadFromFile('C:\fr_script.pas');
MemDataArray[0].OnBeforePrint :='MemoOnBeforePrint'; //动态定义OnBeforePrint事件
iLeft1 := iLeft1 + iWidth;
for i := 1 to 12 do
begin
if Odd(i) then
CreateMemoData(MemDataArray[i], DataBand, MemArray1[i].Left, 0, MemArray1[i].Width,
DataHeight,
FName, iFont, haCenter, vaCenter, frxDBDataset1, arrFields[i, 1],
[ftRight, ftBottom], '', '', fkText)
else
CreateMemoData(MemDataArray[i], DataBand, MemArray1[i].Left, 0, MemArray1[i].Width,
DataHeight,
FName, iFont, haRight, vaCenter, frxDBDataset1, arrFields[i, 1],
[ftRight, ftBottom], ',', '%2.2n', fkNumeric);
end;
frReport.PrepareReport();
frReport.ShowReport();
end;
C:\fr_script.pas中写入脚本
procedure MemoOnBeforePrint(Sender: TfrxComponent);
begin
if Length(TfrxMemoView(Sender).Memo.Text)>10 then
begin
TfrxMemoView(Sender).Memo.Text :=Trim(TfrxMemoView(Sender).Memo.Text);
TfrxMemoView(Sender).HAlign :=haLeft;
//TfrxMemoView(Sender).VAlign :=VaTop;
end;
end;
begin
end.
本文来自博客园,作者:del88,转载请注明原文链接:https://www.cnblogs.com/del88/archive/2012/01/05/2313422.html