Tag-Group
Group:一筆資料就是爲一個group.
Group可以嵌套循環。
<?end for-each?>做爲一個Group的結尾,放在資料的最後一個Field的後面。
Tag-<?<Field Name>?>
<?sort:<Item_Column1>?><?sort:<Item_Column2>?>
例如:<?sort:PO_NUM;'descending';data-type='text'?>
url:{IMAGE_LOCATION}( IMAGE_LOCATION 爲group中一元素)
url:{’${OA_MEDIA}/image name’}( OA_MEDIA->爲一個目錄)
例如:url:{’${OA_MEDIA}/ORACLE_LOGO.gif’}
充分利用Word的格式化功能:紙張自定義、顏色、字號、字體、標題樣式、背景、水印、對齊、表格、分欄、模版日期、窗體域的數字/日期格式、文件圖片、頁眉頁腳、自動圖文集。雖然可以代碼控制,但既然依託Word作爲可視化設計工具,我們就儘量使用Word功能來設計報表吧!
1.2. Word表格
和做網頁一樣,表格在報表佈局中的地位至關重要,要熟練掌握,尤其是:
1、 標題行重複,可以實現新頁重複標題。
2、 嵌套表格、行列合併、邊框、底紋,可以實現特殊的佈局。
3、 固定列寬、自動調整、禁止跨頁斷行,可以實現一些嚴格的佈局控制。
1.3.行截斷與禁止折行
單據打印中對格式的要求比較高,如果某一行過長或者出現多次折行,就會破壞版面,尤其是套打等要求較高的場合,這裏把各種方法作個小結。
1.3.1 Word功能,不理想
固定列寬功能可以用,但固定行高不行,雖然設計時看到“固定”了,如果不加控制,運行後多出列寬的數據會自動折行。
1.3.2 單行+截斷,即控制只有一行,多餘截斷,禁止折行
在字段後,再加兩個命令:
<xsl:attribute xdofo:ctx="block" name="wrap-option">no-wrap</xsl:attribute>
<xsl:attribute xdofo:ctx="block" name="overflow">hidden</xsl:attribute>
參見:行截斷與禁止折行.rtf
行截斷與禁止折行.xml
1.3.2.1行不加任何控制不截段
見圖2.2 ,紅圈所劃的地方
Industry Id
Industry
Year
Month
Sales
FINDUSTRY_ID
INDUSTRY
YEAR
MONTH
SALES
E
圖2.1
圖2.2
看看圖2.1中各個窗體字段的幫助文本,(雙擊該字段,在彈出窗口中單擊添加幫助文字即可查看),如表2.1。
窗體字段(從上到下,從左到右) | 幫助文本 |
F | <?for-each:ROW?> |
INDUSTRY_ID | <?INDUSTRY_ID?> |
INDUSTRY | <?INDUSTRY?> |
YEAR | <?YEAR?> |
MONTH | <?MONTH?> |
SALES | <?SALES?> |
E | <?end for-each?> |
表2.1
1.3.2.2行+截斷,即控制只有一行,多餘截斷,禁止折行
顯示效果,見圖2.3
實現方法,見表2.2
窗體字段(從上到下,從左到右) | 幫助文本 |
F | <?for-each:ROW?> |
INDUSTRY_ID | <?INDUSTRY_ID?> |
INDUSTRY | <?INDUSTRY?> <xsl:attribute xdofo:ctx="block" name="wrap-option">no-wrap</xsl:attribute><xsl:attribute xdofo:ctx="block" name="overflow">hidden</xsl:attribute> |
YEAR | <?YEAR?> |
MONTH | <?MONTH?> |
SALES | <?SALES?> |
E | <?end for-each?> |
1.3.3 多行+截斷,難
如固定顯示3行,多餘部分截斷,目前通過模版無法實現,只有在數據源中先將數據截至剛好3行的字符數,然後利用自動折行功能。這裏還要注意空格,如果遇到空格,後面的單詞又顯示不下,將會提前自動折行。
1.4.條件格式化
在不同的條件下顯示不同的顏色、不同的列數、不同的標題、不同的佈局風格等等,這些都屬於條件格式化,需要藉助IF命令。
1、比如不同幣種憑證打印格式不同
<?if:CURRENCY_CODE="CNY"?>任何佈局<?end if?>
<?if:CURRENCY_CODE!="CNY"?>任何佈局<?end if?>
2、比如貨幣爲CNY時才顯示列,在目標列的單元格內寫如下語句
<?if@column:CURRENCY_CODE="CNY"?>字段值和格式<?end if?>
注:這還不是真正的動態列。
3、比如偶數行底色爲灰色,在行的任何單元格內寫如下語句
<?if@rowposition() mod 2=0?>
<xsl:attribute xdofo:ctx="incontext" name="background-color">gray</xsl:attribute>
<?end if?>
4、
比如超過100單元格呈紅色,在目標單元格內寫如下語句
<?ifUANTITY>100?>
<xsl:attribute xdofo:ctx="block" name="background-color">red</xsl:attribute>
<?end if?>
2.字段計算技巧
2.1.建議做法
1、
計算字段可以在SQL中先完成。
2、
如果使用Oracle Reports做數據源,那麼計算字段、統計字段也可以先完成。
3、
在SQL中的數據,都不帶格式,格式在模版中設置;需要在模版中完成計算的字段,必須不帶格式,主要指數字不能帶千位符號。
2.2.組內合計
N: Template Builder/插入/字段
嚮導可以完成基本的統計,目前支持分組內的:Sum、Count、Min、Max、Avarage。
自動生成的代碼示例:<?sum (QUANTITY)?>。
2.3. 頁內合計
要實現本頁合計數,需分兩步:聲明合計變量、顯示合計變量(可帶格式)。
1、對QUANTITY進行本頁合計,聲明變量QTYTOTAL,注意寫在QUANTITY對應的組內,不然引用不到
<?add-page-totalTYTOTAL;’QUANTITY’?>
2、可在任意地方顯示合計數
<?show-page-totalTYTOTAL?>
那麼如何實現組內+頁內合計呢?
2.4. 結轉合計
把上頁的合計數顯示到下頁,與“頁內合計”類似,需分兩步:聲明合計變量、顯示合計變量。
用得少,可參考User Guide“Brought Forward/Carried Forward Totals”部分。
2.5. 累計數Running Totals
累計每行數字,實際上是這樣完成的:先聲明一個變量,初始化爲0;累加;在需要的地方顯示累計。
1、在分組標記前初始化,Set變量
<?xdoxslt:set_variable($_XDOCTX,’RTotalVar’, 0)?>
2、計算累計值,通常寫在欲累計的字段同一單元格內,比如下面的QUANTITY
<?xdoxslt:set_variable($_XDOCTX,‘RTotalVar’, xdoxslt:get_variable($_XDOCTX,’RTotalVar’) + QUANTITY)?>
3、任意地方顯示累計值,Get變量
<?xdoxslt:get_variable($_XDOCTX,’RTotalVar’)?>
3.任何Page相關問題
3.1. 新組分頁
分頁是自然的,但如果想在某處強制分頁如新組新頁,那麼可以使用Word的分頁符(CTRL+ENTER快捷鍵),但會導致最後出現空白頁;這樣只能使用如下幾種方式:
1、分組聲明中加@section,如<?for-each@section:G_PO_HEADER?>。
2、
<?end for-each?>前加<?split-by-page-break:?>。這個翻譯後,實際上是:
<xsl:if test="position()<last()">
<xsl:attribute name="break-before">page</xsl:attribute>
</xsl:if>
3、 <?end for-each?>前加<xsl:attribute name="break-after">page</xsl:attribute>,此法下RTF最後無空白頁,但PDF有空白頁。
4、 <?end for-each?>前加<xsl:attribute name="break-before">page</xsl:attribute>此法下RTF、PDF最後都有空白頁。
3.2.條件分頁、固定行分頁
1、
任意條件分頁,需要藉助IF
+ 上面的break-after或者break-before,如:
<?if:CURRENCY_CODE="CNY"?>
<xsl:attribute name="break-before">page</xsl:attribute>
<?end if?>
2、
固定行分頁,需要藉助IF
+ 上面的break-after或者break-before,在行<?end
for-each?>前,如下語句控制每頁5行:
<?ifosition() mod 5 =0?>
<xsl:attribute name="break-before">page</xsl:attribute>
<?end if?>
3.3. 頁眉頁腳
1、標準的頁眉頁腳,即單個頁眉頁腳,使用Word的功能即可。
2、擴展的頁眉頁腳,可使用<?start:body?><?end body?>把主體部分“框”起來,凡是在這兩個標記之外的東西,都將被當作頁眉頁腳。
3.4. 頁碼和頁數
1、可以用Word的“自動圖文集”,在任意地方插入頁碼,這個是“自然頁碼”。
2、如果在某種情況下想讓頁碼從特定值開始,比如新的組頁碼重新編號,則需要藉助命令,如在for-each後寫:<?initial-page-number:1?>。這裏的“1”,實際上也可以用數據文件中的XML元素來替換。
3.5. 末頁、奇偶頁不同
1、 Word可在頁眉頁腳部分實現首頁不同或奇偶頁不同,沒法實現末頁不同,即使藉助代碼控制,實際實現的也是末頁佈局不同,而非“頁眉頁腳”不同。
<?start@last-page:body?><?end body?>
報表本身僅有一頁時,則用<?start@last-page-first:body?><?end body?>
例子“Advanced\Last Page”,注意佈局需要獨立成頁,即之前需要加分頁符。
2、
以偶數頁結束,主要目的是顯示偶數頁頁眉頁腳
<?section:force-page-count;’end-on-even-layout’?>
如果僅顯示空白頁,則用<?section:force-page-count;’end-on-even’?>
3、
以奇數頁結束,主要目的是顯示奇數頁頁眉頁腳
<?section:force-page-count;’end-on-odd-layout’?>
如果僅顯示空白頁,則用<?section:force-page-count;’end-on-odd’?>
4.使用多媒體元素
4.1. Word功能
可以使用公式、繪圖(如組織結構圖、線條等)、藝術字、剪貼畫。
如果想通過代碼在有限範圍內控制這些對象,比如顯示文字、縮放、旋轉、移動、複製,可參考User Guide中的“Drawing, Shape and Clip Art Support”。
4.2. 複選框
插入複選框窗體域,因其選中代表True,不選代表False,需要我們輸入條件表達式,如:0。
4.3. 下拉框
插入下拉框窗體域,定義下拉框的元素,並同樣在“自己鍵入”內輸入需要引用的XML標記如<%AREA_INDEX%>。這裏要注意元素的順序,因爲是用順序號和運行時的值進行匹配的,也就是XML數據中,AREA_INDEX是自然數1、2……。
4.4. 超鏈接
可以直接利用Word功能設置超鏈接,也可以在鏈接地址中,全部或部分引用XML數據文件中的標記,做到動態超鏈接:
{SUPPLIER_SITE_URL}或者
http://huajhua.leiko.com:8000/OA_MEDIA/{CURRENCY_CODE}.gif
4.5. 圖片
可以直接利用Word功能插入圖片,也可以僅將該圖片當作佔位圖,在圖片的“設置圖片格式”的網站標籤頁內的“可選文字”,輸入真正的圖片地址:
1、來自網站的圖片:url:{‘http://localhost:8000/OA_MEDIA/forms_logo.gif’}。
2、來自EBS的圖片:url:{‘${OA_MEDIA}/forms_logo.gif’}。
3、動態指定地址:url:{IMAGE_URL}。
4、動態拼接的地址:url:{concat(SERVER,’/’,IMAGE_DIR,’/’,IMAGE_FILE)}。
5、直接來自內容爲BLOB的XML元素,僅用於“Data Templates”:
<fo:instream-foreign-object content-type="image/jpg">
<xsl:value-of select="IMAGE_ELEMENT"/>
</fo:instram-foreign-object>
4.6.圖表
可使用Template Builder嚮導插入圖表,類型有:條形圖-垂直、條形圖-水平、餅圖、線形圖。
嚮導生成的代碼,可在圖片的“設置圖片格式”網站標籤頁內的“可選文字”裏看到,我們可以做進一步修改。
5.RTF示例彙總
5.1、
組內循環
<?for-each:G_INVOICES?>
<?end for-each?>
5.2、if條件
<?if:1=2?>
<?end if?>
5.3、Repeatable Row Headers
If your invoices break across a page and you want to repeat the column header row this can be achieved using MSWord’s functionality.
1.
Highlight the header row
2.
Table > Table Properties
3.
Under the Row tab select the ‘Repeat as Header row at the top of each page’
If the invoices table breaks across a page the header row will be repeated.
Splitting Rows across pages
You can prevent rows splitting across a page break by using MSWord’s funtionality:
1.
Highlight the data row
2.
Table > Table Properties
3.
Under the Row tab deselect the ‘Allow row to break across pages’
Rows that would normally be split across two pages will now be moved to the second page to preserve the row data.
Allow row to break across pages不打勾
llow row to break across pages
打勾
5.4.
I
Invoice Listing by Supplier 2
Now lets combine the Supplierswith their Invoices, we will create an output that lists each Supplier and then their Invoices.
Field Name | Contents | Description |
Grp:Supplier | for-each:G_VENDOR | Used to loop through the Supplier members |
Supplier 1 |
VENDOR_NAME | Supplier Name |
1 Long Avenue |
ADDRESS | Address |
Grp:Invoice |
<?for-each:G_INVOICE_NUM?> | Used to loop through the Invoice group |
1134922 |
<?INVOICE_NUM?> | Invoice Number |
1-Jan-2004 |
<?INVOICE_DATE?> | Invoice Date |
USD |
<?INVOICE_CURRENCY_CODE?> | Invoice Currency Code |
1-Jan-2004 |
<?GL_DATE?> | GL Date |
1000.00 |
<?ENT_AMT?> | Entered amount |
1000.00 |
<?ACCT_AMT?> | Accounted Amount |
End Invoice |
<?end for-each?> | Ends the Invoice loop |
End Supplier | end for-each | Ends the Supplier loop |
To achieve this master/detail layout we need to nest the looping fields:
Grp: Supplier
…
Grp: Invoices
…
End Invoices
…
End Supplier
Below you’ll see this layout to achieve the desired finalreport layout. Of course we can use the tips and tricks mentioned in previous lessons to row stripe, row splitting, etc.
I
Grp:Supplier
Supplier: | Supplier 1 |
Address: | 1 Long Avenue |
Invoice NumInvoice DateCurrGL DateEntered
AmtAccounted
Amt
Grp:Invoice
1134922
1-Jan-2004
USD
1-Jan-2004
1000.00
1000.00
End Invoice
End Supplier
5.5.
彙總值
<?sum(ENT_AMT)?> and sum(ACCTD_AMT)?>
Grp:Supplier
Supplier: | Supplier 1 |
Address: | 1 Long Avenue |
Invoice Num | Invoice Date | Curr | GL Date | Entered Amt | Accounted Amt |
Grp:Invoice1134922 | 1-Jan-2004 | USD | 1-Jan-2004 | 1000.00 | 1000.00End Invoice |
Total for Supplier:Supplier
1 |
1000.00 | 1000.00 |
End Supplier
Report Total | SUM_ENT_AMT |
1000.00 |
SUM
5.6.Number Formatting
string format-number(number,format,[decimalformat])
<?format-number(ENT_AMT,"#,###.00")?>
5.7.Grouping
for-each-group:G_INVOICE_NUM;INVOICE_CURRENCY_CODE
You may need to further group your data in addition to the existing grouping, in this case we have:
SUPPLIERS
INVOICES
Rather than update the XML extraction, we can actually introduce a new group to the hierarchy in the template layer so we have:
SUPPLIERS
CURRENCY
INVOICES
We can use a new function, ‘for-each-group’ to effectively regroup the data in the template to show the invoices by currency code, we can then show summary totals at the currenct level too. In the template below you notice that it has been restructured from the previous version. There is now a new table:
<?for-each-group:G_INVOICE_NUM;INVOICE_CURRENCY_CODE?>
<?for-each:current-group()?>
ReGrp:Curr Currency: USD | |
Invoice NumTypeInvoice DateGL DateEntered AmtAccounted Amt | |
Grp:Invoice1134922 Standard 1-Jan-20041-Jan-20041000.001000.00End Invoice |
|
Total for USD1000.001000.00 |
5.8 shapes
<?for-each@shape:SALE?>
<?shape-offset-xposition()-1)*110?>
<?end for-each?>
<?shape-size-y:SERVICES div 1000;'left/bottom'?>
<?shape-size-y:SOFTWARE div 1000;'left/bottom'?>
5.9
if:position() mod 2=0
I Table 3: If you would like a striped effect on your rows this can be achieved in the template. We now use two almost identical data rows in the table. The native XSL position() function is used to assign each row a numeric value, an ‘if’ statement is then used to test the value, so at the beginning of each row we get an if statement:
1st Row: if:position() mod 2=0
2nd Row: if:position() mod 2=1
The position() function will return a number for the row of data starting at ‘1’. The mod or modulus function will return a 0 or a 1 depending on the result e.g. 2 mod 2 returns 0 and 3 mod 2 returns a 1. So we shade each row as required and add the if statement and at runtime the rows will be alternately shaded, in this case white and gray.
I
Table:3 – Row Striping
Supplier | Address |
Grp:SupplierSupplier 1 | 1 Long AvenueEndIf |
IfSupplier 1 | 1 Long AvenueEnd |
ITable 4: You can also use Word’s Table Auto Format feature to apply astyle to
your tables. Highlight the table then Table >
Table AutoFormat then select the style you want from the dialog.I
Table:4 – Auto Formatting
Supplier | Address |
Grp:SupplierSupplier 1 | 1 Long AvenueEnd |
5.10
Brought Forward
<?call:header?>
Call:header
是調用
<?template:header?>
<?template:header?>
Invoice Listing Report |
1000 | 1000 |
<xdofo:inline-total display-condition="exceptfirst" name="EntAmt">Brought Forward: <xdofo:show-brought-forward name="EntAmt" format="99G999G999D00"/></xdofo:inline-total>
Brought Forward:本頁之前的彙總值
帶到下一頁
<xdofo:inline-total display-condition="exceptfirst" name="EntAmt"><xdofo:show-brought-forward name="AcctAmt" format="99G999G999D00"/></xdofo:inline-total>
<?end template ?>
<?template:footer?>
Page Total: | 1000 | 1000 | |
<?show-page-total:EntAmt;"99G999G999D00"?> | <?show-page-total:AcctAmt;"99G999G999D00"?> | ||
1000 | 1000 | ||
<xdofo:inline-total display-condition="exceptlast" name="EntAmt">Carried Forward:
<xdofo:show-carry-forward name="EntAmt" format="99G999G999D00"/></xdofo:inline-total>
<xdofo:inline-total display-condition="exceptlast" name="AcctAmt"><xdofo:show-carry-forward name="AcctAmt" format="99G999G999D00"/></xdofo:inline-total>
Carried Forward
接後,轉下頁、結轉
forward
<?end template?>
InvoiceTypeInvoiceNumberInvoiceDateInvoiceCurrencyEntered AmountAccountedAmount
FE
INV_TYPE
13234210-May-2005USD1,000.001,000.00EG
<?call:footer?>
5.11Conditonal
條件
Setting font to bold if invoice amount greater than 1000.
InvoiceNumber | Inovice Amount |
Grp: Invoice13222-2 | CH$100.00EFE |
<?for-each:G_INVOICE_NUM?>
<?if:ACCTD_AMT>1000?><xsl:attribute xdofo:ctx="block" name="font-weight">bold</xsl:attribute><?end if?>
ACCTD_AMT》1000 的數據 字體加粗
Setting background color to green if invoice amount greater than 1000.
InvoiceNumber | Inovice Amount |
Grp: Invoice13222-2 | CH$100.00EFE |
<?if:ACCTD_AMT>1000?><xsl:attribute xdofo:ctx="block" name="background-color">lime</xsl:attribute><?end if?>
ACCTD_AMT》1000
的數據
顏色爲綠色