在RDLC中,可以使用自定義方法來擴充報表功能。比如常見的,把數(shù)字轉(zhuǎn)換為大寫中文的需求。按如下方法操作。
1、在報表空白處先一點,確保沒點中任何報表對象。然后點擊菜單欄上的“報表”。
選“報表屬性”,在彈出的小窗口上,切換到“代碼”標簽頁。
在文本框內(nèi)輸入你的函數(shù),我們這里輸入了一個CNMoney函數(shù)。
注意:
(1)嵌入代碼中的方法必須以 Microsoft Visual Basic 語法進行編寫
(2)代碼塊可以包含多個方法。
(3)無法向函數(shù)傳遞數(shù)據(jù)值集,不支持自定義聚合。一般用于簡單數(shù)據(jù)類型。
(4)此方法可在該報表中多次使用,但是不能脫離報表使用。即不能在報表中共享這一函數(shù)。
其他報表中要用到該方法,需在報表中創(chuàng)建同樣的代碼段。
2、在報表上拖一個文本框,在上面點右鍵,選“表達式”,在彈出的表達式界面上,輸入
code.CNMoney(16)
注意:以code+.+函數(shù)名稱的方式引用之前定義的方法
下圖為操作流程示意

下面為文中用到的函數(shù)體,可在項目中實際使用。
- '#############################################################################
- '貨幣轉(zhuǎn)換為中文漢字表述
- '
- '函數(shù)名稱:CNMoney
- '參數(shù):ls
- '返回值:轉(zhuǎn)換后的字符串
- '
- '整理人:阿泰
- '版本歷史
- '2010-04-20:首次編譯,修正0參數(shù),修正小于10的值不能正常顯示的BUG
- '
- '本文函數(shù)來源于 feng442624978,原帖地址:
- 'http://topic.csdn.net/u/20100303/15/0f0ceca7-d29d-4269-b0f5-17ea09d0f139.html
- '#############################################################################
-
- Shared Function CNMoney(ls As Long) As String
- Dim dx_sz As String
- Dim dx_dw As String
- Dim str_int As String
- Dim str_dec As String
- Dim dx_str As String
- Dim fu As String
- Dim a As String
- Dim b As String
- Dim c As String
- Dim d As String
- Dim b2 As String
- Dim num_int As Long
- Dim num_dec As Long
- Dim len_int As Long
- Dim i As Long
- Dim a_int As Long
- Dim pp As Long
-
- dx_sz = "零壹貳叁肆伍陸柒捌玖"
- dx_dw = "萬仟佰拾億仟佰拾萬仟佰拾圓"
-
- If ls = 0 Then
- CNMoney = "零圓整"
- Exit Function
- End If
-
- If ls < 0 Then
- ls = Abs(ls)
- fu = "負"
- Else
- fu = ""
- End If
-
- dx_str = CStr(ls)
- dx_str = Replace(dx_str, "¥", "")
- dx_str = Replace(dx_str, ",", "")
- If (ls >= 0) And (ls < 1) Then dx_str = "0" + dx_str
-
- pp = InStr(dx_str, ".")
- If pp > 0 Then
- str_int = Mid(dx_str, 1, InStr(dx_str, ".") - 1)
- Else
- str_int = dx_str
- End If
-
- num_int = CLng(str_int)
-
- If (ls > 0) And (ls < 1) Then
- num_dec = ls * 100
- Else
- num_dec = (ls - num_int) * 100
- End If
-
- str_dec = CStr(num_dec)
- str_dec = Replace(str_dec, "¥", "")
-
- len_int = Len(str_int)
- dx_str = ""
- For i = 1 To len_int
- a = Mid(str_int, i, 1)
- a_int = CLng(a)
- b = Mid(dx_sz, (a_int + 1), 1)
- c = Mid(dx_dw, (13 - len_int + i), 1)
- If dx_str <> "" Then
- d = Mid(dx_str, Len(dx_str) - 1, 1)
- Else
- d = ""
- End If
- If (b = "零") And ((d = "零") Or (b = b2) Or (c = "圓") Or (c = "萬") Or (c = "億")) Then b = ""
- If (a = "0") And (c <> "圓") And (c <> "萬") And (c <> "億") Then c = ""
- If ((c = "圓") Or (c = "萬") Or (c = "億")) And (d = "零") And (a = "0") Then
- dx_str = Mid(dx_str, 1, Len(dx_str) - 2)
- d = Mid(dx_str, Len(dx_str) - 1, 2)
- If ((c = "圓") And (d = "萬")) Or ((c = "萬") And (d = "億")) Then c = ""
- End If
- dx_str = dx_str + b + c
- b2 = b
- Next i
-
- '處理金額小于1的情況
- If Len(dx_str) < 2 Then dx_str = ""
- If (num_dec < 10) And (ls > 0) Then
- a_int = CLng(str_dec)
- b = Mid(dx_sz, (a_int + 1), 1)
- If num_dec = 0 Then dx_str = dx_str + "整"
- If num_dec > 0 Then dx_str = dx_str + "零" + b + "分"
- End If
- If num_dec >= 10 Then
- a_int = CLng(Mid(str_dec, 1, 1))
- a = Mid(dx_sz, (a_int + 1), 1)
- a_int = CLng(Mid(str_dec, 2, 1))
- b = Mid(dx_sz, (a_int + 1), 1)
- If a <> "零" Then a = a + "角"
- If b <> "零" Then b = b + "分" Else b = ""
- dx_str = dx_str + a + b
- End If
-
- dx_str = fu + dx_str
-
- dx_str = Replace(dx_str, "零億", "億")
- dx_str = Replace(dx_str, "零萬", "萬")
- dx_str = Replace(dx_str, "零千", "千")
- dx_str = Replace(dx_str, "零圓", "圓")
-
-
- CNMoney = dx_str
- End Function
如果函數(shù)有錯誤,在編譯時會出現(xiàn)類似的提示信息

可根據(jù)提示進行修正
注:本文為在報表中使用自定義函數(shù)的方法之一,之后有時間補充其他方法。