RDLC报表中使用自定义函数

 

在RDLC中,可以使用自定义方法来扩充报表功能。

比如常见的,把数字转换为大写中文的需求。按如下方法操作。

 

1:在报表空白处先一点,确保没点中任何报表对象。然后点击菜单栏上的“报表”。

选“报表属性”,在弹出的小窗口上,切换到“代码”标签页。

 

 

在文本框内输入你的函数,我们这里输入了一个CNMoney函数。

 

 

注意:

(1)嵌入代码中的方法必须以 Microsoft Visual Basic 语法进行编写

(2)代码块可以包含多个方法。

(3)无法向函数传递数据值集,不支持自定义聚合。一般用于简单数据类型。

(4)此方法可在该报表中多次使用,但是不能脱离报表使用。即不能在报表中共享这一函数。

 其他报表中要用到该方法,需在报表中创建同样的代码段。

 

2:在报表上拖一个文本框,在上面点右键,选“表达式”,在弹出的表达式界面上,输入

code.CNMoney(16)

 

 

注意:

以code+.+函数名称的方式引用之前定义的方法

 

 

下图为操作流程示意

 

 

下面为文中用到的函数体,可在项目中实际使用。

'############################################################################# '货币转换为中文汉字表述 ' '函数名称:CNMoney '参数:ls '返回值:转换后的字符串 ' '整理人:阿泰 '版本历史 '2010-04-20:首次编译,修正0参数,修正小于10的值不能正常显示的BUG ' '本文函数来源于 feng442624978,原帖地址: 'http://topic.csdn.net/u/20100303/15/0f0ceca7-d29d-4269-b0f5-17ea09d0f139.html '############################################################################# Shared Function CNMoney(ls As Double) 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

 

 

如果函数有错误,在编译时会出现类似的提示信息

 

可根据提示进行修正

 

 

注:

本文为在报表中使用自定义函数的方法之一,之后有时间补充其他方法。

 

修正:

感谢何金辉同学的提醒,第一版函数中,输入参数类型设置为Long了,所以丢失了小数点部分的角和分。修正为Double。

 

函数做一下修正:

Function CNMoney(money As Double) As String Dim str_Money=money.ToString() Dim str_China As [String]() = {"分", "角", "元", "拾", "佰", "仟", _ "万", "拾", "佰", "仟", "亿", "拾", _ "佰", "仟", "兆", "拾", "佰", "仟"} Dim str_Number As [String]() = {"零", "壹", "贰", "叁", "肆", "伍", _ "陆", "柒", "捌", "玖"} Dim M As [String] = "" Dim isPoint As Boolean = False If str_Money.IndexOf(".") <> -1 Then str_Money = str_Money.Remove(str_Money.IndexOf("."), 1) isPoint = True End If For i As Integer = str_Money.Length To 1 Step -1 Dim MyData As Integer = Convert.ToInt16(str_Money(str_Money.Length - i).ToString()) M += str_Number(MyData) If isPoint = True Then M += str_China(i - 1) Else M += str_China(i + 1) End If Next Return M End Function  

 

你可能感兴趣的:(RDLC)