0
点赞
收藏
分享

微信扫一扫

FastReport VCL报表通过代码创建报告表单教程


FastReport VCL是用于Delphi,C ++ Builder,RAD Studio和Lazarus的报告和文档创建VCL库。它提供了可视化模板设计器,可以访问最受欢迎的数据源,报告引擎,预览,将过滤器导出为30多种格式,并可以部署到云,Web,电子邮件和打印中。

近日,FastReport VCL更新至v6.9,在新版本中,在PDF导出中增加了对以下对象的交互式表单支持:文本,复选框和图片。能够通过InteractiveFormsFontSubset属性将所需的字形仅包含在交互式形式中。同时修复了多个Bug问题。欢迎下载体验。(点击下方按钮下载)


通常,您将使用设计器创建大多数报告。但是,在某些情况下(例如,当报告的表单未知时),有必要通过代码手动创建报告。

要手动创建报告,应按顺序执行以下步骤:

清除报告组件
添加数据源
添加“数据”页面
添加报告页面
在页面上添加乐队
设置频段的属性,然后将其连接到数据
在每个频段上添加对象
设置对象的属性,然后将它们连接到数据
让我们检查一下“列表”类型的简单报告的创建。假定我们具有以下组件:frxReport1:TfrxReport和frxDBDataSet1:TfrxDBDataSet(最后一个连接到DBDEMOS的数据,«Customer.db»表)。我们的报告将包含一页带有“报告标题”和“主数据”带的页面。在“报告标题”区域中,将出现一个带有“ Hello FastReport!”的对象。文本,而“主数据”将包含一个对象,该对象具有指向“ CustNo”字段的链接。
Pascal:

var
DataPage: TfrxDataPage;
Page: TfrxReportPage;
Band: TfrxBand;
DataBand: TfrxMasterData;
Memo: TfrxMemoView;

{ clear a report }
frxReport1.Clear;

{ add a dataset to the list of ones accessible for a report }
frxReport1.DataSets.Add(frxDBDataSet1);

{ add the "Data" page }
DataPage := TfrxDataPage.Create(frxReport1);

{ add a page }
Page := TfrxReportPage.Create(frxReport1);

{ create a unique name }
Page.CreateUniqueName;

{ set sizes of fields, paper and orientation by default }
Page.SetDefaults;

{ modify paper’s orientation }
Page.Orientation := poLandscape;

{ add a report title band}
Band := TfrxReportTitle.Create(Page);
Band.CreateUniqueName;

{ it is sufficient to set the «Top» coordinate and height for a band }
{ both coordinates are in pixels }
Band.Top := 0;
Band.Height := 20;

{ add an object to the report title band }
Memo := TfrxMemoView.Create(Band);
Memo.CreateUniqueName;
Memo.Text := 'Hello FastReport!';
Memo.Height := 20;

{ this object will be stretched according to band’s width }
Memo.Align := baWidth;

{ add the masterdata band }
DataBand := TfrxMasterData.Create(Page);
DataBand.CreateUniqueName;
DataBand.DataSet := frxDBDataSet1;

{ the Top coordinate should be greater than the previously added band’s top + height}
DataBand.Top := 100;
DataBand.Height := 20;

{ add an object on master data }
Memo := TfrxMemoView.Create(DataBand);
Memo.CreateUniqueName;

{ connect to data }
Memo.DataSet := frxDBDataSet1;
Memo.DataField := 'CustNo';
Memo.SetBounds(0, 0, 100, 20);

{ adjust the text to the right object’s margin }
Memo.HAlign := haRight;

{ show the report }
frxReport1.ShowReport;

C ++:

TfrxDataPage * DataPage;
TfrxReportPage * Page;
TfrxBand * Band;
TfrxMasterData * DataBand;
TfrxMemoView * Memo;

// clear a report
frxReport1->Clear();

// add a dataset to the list of ones accessible for a report
frxReport1->DataSets->Add(frxDBDataset1);

// add the "Data" page
DataPage = new TfrxDataPage(frxReport1);

// add a page
Page = new TfrxReportPage(frxReport1);

// create a unique name
Page->CreateUniqueName();

// set sizes of fields, paper and orientation by default
Page->SetDefaults();

// modify paper’s orientation
Page->Orientation = poLandscape;

// add a report title band
Band = new TfrxReportTitle(Page);
Band->CreateUniqueName();

// it is sufficient to set the «Top» coordinate and height for a band
// both coordinates are in pixels
Band->Top = 0;
Band->Height = 20;

// add an object to the report title band
Memo = new TfrxMemoView(Band);
Memo->CreateUniqueName();
Memo->Text = "Hello FastReport!";
Memo->Height = 20;

// this object will be stretched according to band’s width
Memo->Align = baWidth;

// add the masterdata band
DataBand = new TfrxMasterData(Page);
DataBand->CreateUniqueName();
DataBand->DataSet = frxDBDataset1;

// the Top coordinate should be greater than the previously added band’s top + height
DataBand->Top = 100;
DataBand->Height = 20;

// add an object on master data
Memo = new TfrxMemoView(DataBand);
Memo->CreateUniqueName();

// connect to data
Memo->DataSet = frxDBDataset1;
Memo->DataField = "CustNo";
Memo->SetBounds(0, 0, 100, 20);

// adjust the text to the right object’s margin
Memo->HAlign = haRight;

// show the report
frxReport1->ShowReport(true);

让我们解释一些细节。
必须在报告中使用的所有数据源都必须添加到数据源列表中。在我们的情况下,这是使用

frxReport1.DataSets.Add(frxDBDataSet1)

line,否则,报告将不起作用。
对于将内部数据集插入TfrxADOTable到报表中,“数据”页面是必需的。此类数据集只能放置在“数据”页面上。

Page.SetDefaults不需要调用,因为在这种情况下页面将具有А4格式,页边距为0毫米。SetDefaults设置10mm页边距,并采用打印机默认具有的页面尺寸和对齐方式。

在页面上添加带区时,应确保它们不会相互重叠。为此,只需设置«Top»和«Height»坐标即可。修改«Left»和«Width»坐标毫无用处,因为带始终具有其所在页面的宽度(如果是垂直带,则不正确–您应设置Left和Width属性,并不在乎顶部和高度)。应该注意的是,乐队在页面上的位置顺序非常重要。始终以与设计者相同的方式定位乐队。

对象的坐标和大小以像素为单位设置。因为Left,Top,Width,和Height所有对象的属性有«扩展»类型,你可以指出非整数值。定义了以下常量,用于将像素转换为厘米和英寸:

fr01cm = 3.77953;
fr1cm = 37.7953;
fr01in = 9.6;
fr1in = 96;

例如,可以将带子的高度设置为等于5毫米,如下所示:

Band.Height := fr01cm * 5; 
Band.Height := fr1cm * 0.5;

如果您对FastReport感兴趣,可以在慧都网免费下载最新试用版,欢迎加入FastReport QQ交流群:801349317

举报

相关推荐

0 条评论