一:格式化一個值
聚合函數的一個特征是,返回的數值沒有格式化,如如下例子所示,它使用“SUM”:
數據字段通常返回一個格式化的值,這是一個沒有任何變化的“文本”對象所顯示的值. 格式化“SUM” 的結果, 讓我們用FastReport中的格式化工具.
雙擊報表上的對象,切換到'Display Format...' 頁進行設置:
這個編輯器在左邊顯示格式的類別, 相應的格式字符顯示在右邊. 我們選擇 “Number” 分類和 "$1,234.50" 格式. 格式字符串是“Format”函數的一個參數, FastReport使用來實現數字格式的. 格式字符串和小數分隔符可以改變. 如果將小數分隔符留下空白,則使用當前區域設置值.
點擊后ОК並預覽報表,你會發現現在報表的總和是正確的格式:
Note the combobox at the top of the dialogue form. If we have more than one expression in an object, we may set different formatting for each expression.
二:內部格式化
內部格式化允許你在對象上設置不同的格式化字符串表達式. 這個被用於以前版本的FastReport. 現在是過時的(使用格式化對話為每個表達式設置不同的格式).
使用例子, re-size the footer and its object and change the object text to this:
Total: [SUM(<Group."ItemsTotal">,MasterData1)]
Number: [COUNT(MasterData1)]
The total and the number of orders will be displayed in the object.
In the report preview both of these values are shown in monetary format, which we had previously set. This is incorrect:
To display each value in its correct format they need to be formatted individually. To do this we use format tags, which are placed just before the closing square bracket of the expression. In our example, disable formatting for the object (select “Text (no formatting)” category in the format editor). Now we need to specify the format for just the first expression, as the second one will be displayed correctly by default (i.e. as an integer). Change the object text as follows:
Sum: [SUM(<Group."ItemsTotal">,MasterData1) #n%2,2m]
Number: [COUNT(MasterData1)]
Preview the report to make sure that the object is displayed correctly:
The general syntax of format tags is:
[expression #formattag]
Note that the space character between the expression and the “#” symbol is mandatory! The format tag itself might look like:
#nformat_string – numerical format
#dformat_string – date/time format
#bFalse,True – boolean format
Format_string in each case is the argument to the function used for formatting. So, for numerical formatting the Delphi Format function is used, and for date/time the FormatDateTime function. The syntax for these functions can be found in the Delphi help system. Below are several values used in FastReport:
for numerical formatting:
%g – number with the minimal places after the decimal point
%2.2f – number with a fixed number of places after the decimal point
%2.2n – as previous, but with thousands separator
%2.2m – monetary format, accepted by the Windows OS, dependent on the regional settings in the control panel
for date/time formatting:
dd.mm.yyyy – date as '23.12.2003'
dd mmm yyyy – date as '23 Nov 2003'
dd mmmm yyyy – date as '23 November 2003'
hh:mm – time as '23:12'
hh:mm:ss – time as '23:12:00'
dd mmmm yyyy, hh:mm – date and time as '23 November 2003, 23:12'
A comma or a dash can be used instead of the dot in the format_string for numerical formatting. This symbol is used as the separator between the integer and the fractional parts of the value. Any other character is not allowed.
For formatting with the “#b” type (boolean), the format_string is entered as two values separated by a comma. The first value corresponds to “False” and the second to “True”.
三:有條件的突出
在給定的條件下,可以改變“文本”對象的外觀. 比如, 如果某個對象是負值,則可以用紅色高亮顯示. 這個特性叫"conditional highlighting". 要使用它,選擇文本對象並單擊工具欄中的 按鈕.你將看到以下窗口:
可以設置一個或多個條件,每個條件可以包含以下的樣式:
·frame; 邊框
·fill; 填充
·font; 字體
·object's visibility. 是否可見
點擊 "Add" 按鈕. 你將看到一個表達式編輯器. 這里你可以寫任意返回結果是布爾型的表達式. 多數情況下,使用"Value" 變量, 他表示當打印打印的值.
讓我們看以下例子: 打印產品表中的某個字段:
[Products."UnitsInStock"]
我們想如果值 = 0 打印紅色. 我們創建以下表達式:
Value = 0
在給定的條件下, 我們使用"Value" 變量, 它表示打印的值. 如果有多個表達式, 變量的值就是最后一個表達式的值.你可以使用數據列來取值:
<Products."UnitsInStock"> = 0
配置如下:
當對象值是0時,顯示紅色. 我們添加更多的條件, 如果少於10, 打印黃色. 如下:
Value < 10
在有多個條件情況下, FastReport 檢查所有的條件, 從第一個開始. 如果某個條件滿足, FastReport 應用他的樣式設置, 並停止往下. 所以條件的順序很重要. 以下順序是正確的:
1. Value = 0
2. Value < 10
以下順序工作不正常.
1. Value < 10
2. Value = 0
上面的 "Value = 0" 將不被執行, 因為先滿足第一個條件, 調整順序使用 和
按鈕。
四:用顏色顯示交替的數據行
使用條件突出,很容易創建有一個“帶狀”的報表,數據行交替着色的報表。為了節省時間,讓我們用之前設計的“客戶清單”的例子。
放置一個“文本”對象在數據帶上,並延伸到幾乎所有的帶空間:
訪對象根據數據行來改變顏色. 在對象上增加以下條件:
<Line> mod 2 = 1
選擇一個灰色的顏色作為突出,不太飽和的顏色,但接近白色。現在可以將其他對象添加到第一個空的“文本”對象上的數據帶中:
預覽結果如下: