报表默认保存在项目窗体文件中,大多数状况下,没有更多的操做要深圳市, 所以,你不须要采起特别措施来载入报告.若是你决定保存报表到文件或是数据库中 (这样更灵活, 好比修改报表不用重编译程序), 你可使用 “TfrxReport” 组件是加载和保存方法:数据库
function LoadFromFile(const FileName: String; ExceptionIfNotFound: Boolean = False): Boolean;数组
按文件名加载报表. 第二个参数若是是“True” 而且报表文件没找到, 将报异常. 如要报表加载完成,返回 “True.”缓存
procedure LoadFromStream(Stream: TStream);多线程
从流中加载一个报表ide
procedure SaveToFile(const FileName: String);工具
保存报表到指定的文件ui
procedure SaveToStream(Stream: TStream);this
保存报表到流线程
报表文件的扩展名默认为“FR3”.设计
举例:
frxReport1.LoadFromFile('c:\1.fr3');
frxReport1.SaveToFile('c:\2.fr3');
显示报表设计窗体,调用“TfrxReport.DesignReport” 方法. 须要引用相关单元。
方法"DesignReport" 有两个默认参数。
procedure DesignReport(Modal: Boolean = True; MDIChild: Boolean = False);
第一个参数控制是否以模态方式显示窗体。第二个参数控制是否为MDI子窗体。
举例:
frxReport1.DesignReport;
使用“TfrxReport”组件如下的两个方法启动一个报表:
procedure ShowReport(ClearLastReport: Boolean = True);
启动一个报表,并显示预览窗口. 若是参数“ClearLastReport” 等于“False” 报表将添加到先前建立好的报表后面, 不然先前的报表将清除 (默认).
function PrepareReport(ClearLastReport: Boolean = True): Boolean;
启动一个报表,没有预览窗口. 参数跟方法“ShowReport” 同样. 报表建立完成,返回“True”。
大多数状况, 更多使用第一种方法. 会显示预览窗口, 继续构建新报表.
参数“ClearLastReport” 方便控制如下状况,当须要添加其余报表时 。(批量打印报表等状况)
举例:
frxReport1.ShowReport;
预览窗口显示报表有2个方法: 一个是调用 “TfrxReport.ShowReport” 方法, 一个是调用“TfrxReport.ShowPreparedReport” 方法. 第二个方法中报表构建的动做不执行,但报表已经完成显示. 这意味着,你能够事先构建报表借助“preparereport”方法, 或加载先前从文件加载的报表。
举例:
if frxReport1.PrepareReport then
frxReport1.ShowPreparedReport;
在这种状况下, 报表首先构建完成, 以后显示在预览窗体. 构建一个大报表可能花不少时间, 这就是为何使用“showreport”(准同步方法),比使用“PrepareReport/ShowPreparedReport”更好. 能够指定预览方式,经过“tfrxreport.previewoptions”属性进行设置。
大多数状况, 你从预览窗口打印报表. 手动打印报表,能够调用“TfrxReport.Print”方法, 举例:
frxReport1.LoadFromFile(...);
frxReport1.PrepareReport;
frxReport1.Print;
在同一时间, 弹出对话框,能够设置打印参数,. 你能够设置不显示此对话框,设置“TfrxReport.PrintOptions” 属性。
报表能够在预览窗口中执行此操做. 也能够手动执行 “TfrxReport.PreviewPages” 方法:
function LoadFromFile(const FileName: String; ExceptionIfNotFound: Boolean = False): Boolean;
procedure SaveToFile(const FileName: String);
procedure LoadFromStream(Stream: TStream);
procedure SaveToStream(Stream: TStream);
配置和参数等与相应的TfrxReport方法同样。一个文件中包含已完成的报告,其扩展名默认为"fp3"。
举例:
frxReport1.PreviewPages.LoadFromFile('c:\1.fp3');
frxReport1.ShowPreparedReport;
注意,此种完成的报表在加载完毕后,其预览是经过“showpreparedreport”方法!
能够在预览窗口执行此操做. 也能够手动调用“TfrxReport.Export” 方法. 方法的参数要指定要导出的文件类型,如:
frxReport1.Export(frxHTMLExport1);
导出组件必须可用 (组件放在窗体上) 并设置正确.
FastReport显示报表在一个标准的预览窗体中。 能够建立自定义的预览窗体,使用组件“TfrxPreview”。
使用组件时有两个典型问题. 组件没有处理相关按键(光标, PgUp, PgDown etc) 和鼠标滚动。 让TfrxPreview 的按键能够正常工做,把组件设置成当前的焦点 (好比在窗体的 OnShow 事件写代码处理),以下:
frxPreview.SetFocus;
让TfrxPreview 组件能够响应鼠标滚动操做, 你须要建立 OnMouseWheel 事件处理,并调用 TfrxPreview.MouseWheelScroll 方法,以下:
procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
frxPreview1.MouseWheelScroll(WheelDelta);
end;
某些状况下,在一次打印中须要几个报表,或多个报表放在一个预览窗口. FastReport中提供了工具,容许在已有报表上添加新的报表. «TfrxReport.PrepareReport» 方法有一个布尔型的参数选项 «ClearLastReport» , 默认等于 «True» .此参数定义是否清除以前构建的报表. 如下代码演示加载多个报表:
frxReport1.LoadFromFile('1.fr3');
frxReport1.PrepareReport;
frxReport1.LoadFromFile('2.fr3');
frxReport1.PrepareReport(False);
frxReport1.ShowPreparedReport;
咱们加载第一个报表,并构建他,但不显示. 当咱们用同一对象构建第二个报表时,参数 «ClearLastReport» 等于 «False». 这样容许第二个报表添加到以前报表的后,最后在预览窗口中显示构建好的报表。
A:混合报表中的页码
你可使用«Page,» «Page#,» «TotalPages,» 和«TotalPages#» 等系统变量来显示页码,在混合报表中,这些变量以意义以下:
Page – 当前报表的页码
Page# - 整批报表的页码(意即全局的)
TotalPages – 当前报表的全部页汇总 (a report must be a two-pass one)
TotalPages# - 整批报表的页码汇总.
B:混合报表中组合页
正如上面所说, 报表设计页面中的 PrintOnPreviousPage 属性让你打印时接在前一页, 即 使用前一页的空白空间. 在混合型报表中, 容许你从前一报表的空白开始建立新的报表. 要执行此操做,你在设计每一个连续报表的第一页时,打开 PrintOnPreviousPage 属性。
交互式报表,在预览窗口全部报表对象定义一个鼠标单击的响应。例如,用户能够单击数据行,而且运行新的报表显示数据行的明细信息。
任何报表均可以成为交互式报表. 要执行此操做,你须要建立一个 TfrxReport.OnClickObject 事件处理,举例以下:
procedure TForm1.frxReport1ClickObject(Page: TfrxPage; View: TfrxView;Button: TMouseButton; Shift: TShiftState; var Modified: Boolean);
begin
if View.Name = 'Memo1' then
ShowMessage('Memo1 contents:' + #13#10 + TfrxMemoView(View).Text);
if View.Name = 'Memo2' then
begin
TfrxMemoView(View).Text := InputBox('Edit', 'Edit Memo2 text:', TfrxMemoView(View).Text);
Modified := True;
end;
end;
在事件«OnClickObject» 中, 你还能够作如下事情:
- 修改页或报表对象的内容(所以, «Modified» 标志应该指定,所以该修改将考虑在内);
- 调用«TfrxReport.PrepareReport» 方法重建报表。
在上面例子中, Memo2修改了内容,因此设置了Modified := True.
以一样的方式, 能够定义不一样的响应方式; 好比你能够, 运行一个新的报表. 须要注意如下几点。在FastReport 3 版本中, 一个TfrxReport组件只能在一个预览窗体中显示 (不像FastReport 2.x 版本). 这就是为何要运行报表要在一个单独的TfrxReport对象,或在同一个中,但当前的报表必须清除。
在可单击的对象上给用户一个提示信息,咱们能够修改光标,设置对象的cursor 属性。
可点击的对象有一个细节问题可. 在简单的报表中可使用对象的名字或内容,然而,在复杂状况下不能使用. 例如,«Memo1» 的内容是'12'. 明细表根据这个获取不能数据。 这就是为何你须要主键。 FastReport 容许指定一个字符串, 包含任意思数据 (在这个例子中包含一个主键),全部对象使用«TagStr» 属性保存这个字符串。
让咱们以 FastReportDemo.exe - 'Simple list' demo为例作说明. 这是一个公司客户的列表, 包含有 «client’s name,» «address,» «contact perso,» etc. 数据源是«Customer.db» 表,来自数据库DBDEMOS。 表的主键是«CustNo» 字段, 没有显示在报表上. 咱们决定全部对象用这个键关联到记录, 意即用主键获取数据。要执行此操做, 在Master Data Band 上的全部对象的«TagStr» 属性,输入如下值:
[Customers."CustNo"]
构建报表期间, «TagStr» 属性内容以一样的方式计算, 如同文本对象那样计算; 意即变量的值在全部变量的位置被替换, 在这个特殊状况下的一个变量是被封闭在方括号中的。 类型将包含在«TagStr» 属性中, 一个简单的转换从字符串到整数,将给咱们一个主键的值,这样能够找到须要的记录。
若是主键是组合的 (好比包含了多个字段) ,«TagStr» 属性内容像下面这样:
[Table1."Field1"];[Table1."Field2"]
报表构建之后,«TagStr» 属性包含值:'1000;1', 从中获得主键的值是不难的。
FastReport报表对象(如report page, band, memo-object) 不能从你的代码中直接访问. 须要访问,通«TfrxReport.FindObject» 方法找到对象:
var
Memo1: TfrxMemoView;
…
Memo1 := frxReport1.FindObject('Memo1') as TfrxMemoView; //这里能用as ?
找到以后能够访问对象的属性和方法。访问page对象,经过 «TfrxReport.Pages» 属性:
var
Page1: TfrxReportPage;
Page1 := frxReport1.Pages[1] as TfrxReportPage;
做为一个规则, 多数报表用设计器建立,然而,有些状况(好比报表是未知的) 须要手动建立报表,在代码中。
手动建立报表, 应该按如下顺序步骤:
- clear the report component 清除报表
- add data sources 添加数据源
- add the "Data" page 添加Data 页
- add report’s page 添加一个或多个report 页
- add bands on a page 在页上添加一个或多个band
- set bands’ properties, and then connect them to the data 设置band的属性链接到数据
- add objects on each band 在每一个band上添加须要的对象
- set objects’ properties, and then connect them to the data 设置对象的属性,链接到须要的数据字段
让咱们看看一个简单«list»类型报表的建立,假设咱们有如下组件: frxReport1: TfrxReport 和frxDBDataSet1: TfrxDBDataSet (the last one is connected to data from the DBDEMOS, the «Customer.db» table). 报表将只有一页,包含«Report Title» 和«Master Data» bands.在«Report Title» band 有一个"Hello FastReport!" 文本, 在«Master Data» 包含"CustNo" 字段。
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;
让咱们解释一些细节:
要在报表中使用数据源, 必须把数据源添加到DataSets,本例中调用了«frxReport1.DataSets.Add(frxDBDataSet1)» 。不然, 不能工做。
内部数据如TfrxADOTable能够插入报表的“Data”页,这样的数据集能够放置到“Data”页。
Page.SetDefaults 不是必须的, 本例中使用 À4 格式,边距为 0 mm. SetDefaults 设置了10mm 边距,页大小和对齐方及默认打印机。
在页中添加带, 你应该确保他们不互相重叠. 设置足够的«Top» 和«Height» 坐标。 这里不能修改«Left» 和«Width» 坐标, 由于一个带有页宽度(在垂直带时–你应该设置Left 和Width ,忽略 Top 和 Height)。须要注意的是,在页面上的位置的顺序是很是重要的,老是以一样的方式定位带,在设计器中也以一样的方式。
对象的坐标和大小以像素为单位。因为«Left,» «Top,» «Width,» and «Height» 属性是 «Extended» 类型,你能够设置成一个非整形值。 下面的常量被定义为将像素转换为厘米和英寸(frxClass单元):
fr01cm = 3.77953;
fr1cm = 37.7953;
fr01in = 9.6;
fr1in = 96;
例如, 一个band的高等于 5 mm,你能够设置以下:
Band.Height := fr01cm * 5;
Band.Height := fr1cm * 0.5;
十三:在窗体中经过代码建立对话式报表
十四:修改报表的属性
十五:在代码的帮助下建立报表
The primary example’s code is located in the «FastReport Demos\PrintArray» ( "FastReport Demos\BCB Demos\PrintArray") directory. 让咱们解释几个细节.
To print an array, we use a report with one «Master Data» band, which will be presented as many times, as there are elements in the array. To do this, place a «TfrxUserDataSet» component on the form, and then set it’s properties (it is possible to do it in a code, as shown in our example):
RangeEnd := reCount
RangeEndCount := a number of elements in an array
After that, we connect the data-band to the «TfrxUserDataSet» component. To represent the array element, place a text object with the [element] line inside the «Master Data» band. The «element» variable is filled using a «TfrxReport.OnGetValue» event.
The primary example’s code is located in the «FastReport Demos\PrintStringList» ( «FastReport Demos\BCB Demos\PrintStringList») directory. The method is the same, as in the example with an array.
十八:打印一个文件
十九:打印一个TStringGrid
二十:打印一个TTable和TQuery
二十一:报表继承
FastReport能够在不一样的线程独立运做,但有一些特色:
- 即便在不一样的线程,你不能建立TfrxDBDataSet, 由于全局列表"global list" 被用于搜索,当访问会发生在第一次建立TfrxDBDataSet对象时(你能够关闭使用全局列表,默认状况下它是激活的);
- 若是在报表执行过程当中有一些对象属性作了变化(好比在脚本中:Memo1.Left := Memo1.Left + 10), 你须要记住在接下来的操做中,若是TfrxReport.EngineOptions.DestroyForms := False 报表模板将准备修改并须要从新加载或者使用TfrxReport.EngineOptions.DestroyForms := True。 在更新过程当中,您不能在线程中使用交互报表, 由于脚本对象在更新后被删除, 这就是为何在某些状况下,使用TfrxReport.EngineOptions.DestroyForms:=False和更新本身的模板在下一个构建周期中。
If necessary the global list due to which you can search the needed copies of TfrxDBDataSet can be switched off.
{DestroyForms can be switched off, if every time you renew a report from a file or from a current}
FReport.EngineOptions.DestroyForms := False;
FReport.EngineOptions.SilentMode := True;
{ This property switches off the search through global list}
FReport.EngineOptions.UseGlobalDataSetList := False;
{EnabledDataSets plays local list role, you should install it before the template is loaded}
FReport.EnabledDataSets.Add(FfrxDataSet);
FReport.LoadFromFile(ReportName);
FReport.PrepareReport;
(能不用多线程尽可能不用)
The reports and it's data can be cached both in memory ( for speed increasing ) and in file on the disk ( for saving RAM recourses). There are several types of caching in Fast Report:
- TfrxReport.EngineOptions.UseFileCache - if the property is installed in True, than the whole text and objects of built report are saved in temporary file on disk, at that TfrxReport.EngineOptions.MaxMemoSize indicates how many MB are meant for the template in RAM .
- TfrxReport.PreviewOptions.PagesInCache - the number of pages which can be kept in cache memory greatly increases preview speed , but spends much memory ( especially when there are pictures in a template).
- TfrxReport.PreviewOptions.PictureCacheInFile - if the property is on, than all the pictures of built report are saved in temporary file on a disk, that greatly reduces memory use in reports with a large amount of pictures, but it reduces the speed.
FastReport能够建立MDI风格的预览和设计窗体。 The source code of the example is in FastReport Demos\MDI Designer catalogue.
值得一提的是,建议每一个预览窗口或设计窗口建立本身的TfrxReport ,不然全部的窗口都会指向同一个TfrxReport。