当前位置:K88软件开发文章中心编程语言.NET.NET01 → 文章内容

RDLC报表中使用自定义函数

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2018-12-31 11:51:05

:2010-10-16 08:22:00

在RDLC中,可以使用自定义方法来扩充报表功能。比如常见的,把数字转换为大写中文的需求。按如下方法操作。

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

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

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

注意:

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

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

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

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

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

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

code.CNMoney(16)

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

下图为操作流程示意

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

  1. '#############################################################################     
  2. '货币转换为中文汉字表述     
  3. '     
  4. '函数名称:CNMoney     
  5. '参数:ls     
  6. '返回值:转换后的字符串     
  7. '     
  8. '整理人:阿泰     
  9. '版本历史     
  10. '2010-04-20:首次编译,修正0参数,修正小于10的值不能正常显示的BUG     
  11. '     
  12. '本文函数来源于 feng442624978,原帖地址:     
  13. 'http://topic.csdn.net/u/20100303/15/0f0ceca7-d29d-4269-b0f5-17ea09d0f139.html     
  14. '#############################################################################     
  15.      
  16. Shared Function CNMoney(ls As LongAs String     
  17.     Dim dx_sz As String     
  18.     Dim dx_dw As String     
  19.     Dim str_int As String     
  20.     Dim str_dec As String     
  21.     Dim dx_str As String     
  22.     Dim fu As String     
  23.     Dim a As String     
  24.     Dim b As String     
  25.     Dim c As String     
  26.     Dim d As String     
  27.     Dim b2 As String     
  28.     Dim num_int As Long    
  29.     Dim num_dec As Long    
  30.     Dim len_int As Long    
  31.     Dim i As Long    
  32.     Dim a_int As Long    
  33.     Dim pp As Long    
  34.      
  35.     dx_sz = "零壹贰叁肆伍陆柒捌玖"     
  36.     dx_dw = "万仟佰拾亿仟佰拾万仟佰拾圆"     
  37.          
  38.     If ls = 0 Then     
  39.         CNMoney = "零圆整"     
  40.         Exit Function     
  41.     End If     
  42.          
  43.     If ls < 0 Then     
  44.         ls = Abs(ls)     
  45.         fu = "负"     
  46.     Else     
  47.         fu = ""     
  48.     End If     
  49.      
  50.     dx_str = CStr(ls)     
  51.     dx_str = Replace(dx_str, "¥""")     
  52.     dx_str = Replace(dx_str, ",""")     
  53.     If (ls >= 0) And (ls < 1) Then dx_str = "0" + dx_str     
  54.          
  55.     pp = InStr(dx_str, ".")     
  56.     If pp > 0 Then     
  57.         str_int = Mid(dx_str, 1, InStr(dx_str, ".") - 1)     
  58.     Else     
  59.         str_int = dx_str     
  60.     End If     
  61.      
  62.     num_int = CLng(str_int)     
  63.      
  64.     If (ls > 0) And (ls < 1) Then     
  65.         num_dec = ls * 100     
  66.     Else     
  67.         num_dec = (ls - num_int) * 100     
  68.     End If     
  69.      
  70.     str_dec = CStr(num_dec)     
  71.     str_dec = Replace(str_dec, "¥""")     
  72.      
  73.     len_int = Len(str_int)     
  74.     dx_str = ""     
  75.     For i = 1 To len_int     
  76.         a = Mid(str_int, i, 1)     
  77.         a_int = CLng(a)     
  78.         b = Mid(dx_sz, (a_int + 1), 1)     
  79.         c = Mid(dx_dw, (13 - len_int + i), 1)     
  80.         If dx_str <> "" Then     
  81.             d = Mid(dx_str, Len(dx_str) - 1, 1)     
  82.         Else     
  83.             d = ""     
  84.         End If     
  85.         If (b = "零"And ((d = "零"Or (b = b2) Or (c = "圆"Or (c = "万"Or (c = "亿")) Then b = ""     
  86.         If (a = "0"And (c <> "圆"And (c <> "万"And (c <> "亿"Then c = ""     
  87.         If ((c = "圆"Or (c = "万"Or (c = "亿")) And (d = "零"And (a = "0"Then     
  88.             dx_str = Mid(dx_str, 1, Len(dx_str) - 2)     
  89.             d = Mid(dx_str, Len(dx_str) - 1, 2)     
  90.             If ((c = "圆"And (d = "万")) Or ((c = "万"And (d = "亿")) Then c = ""     
  91.         End If     
  92.         dx_str = dx_str + b + c     
  93.         b2 = b     
  94.     Next i     
  95.      
  96.     '处理金额小于1的情况     
  97.     If Len(dx_str) < 2 Then dx_str = ""     
  98.     If (num_dec < 10) And (ls > 0) Then     
  99.         a_int = CLng(str_dec)     
  100.         b = Mid(dx_sz, (a_int + 1), 1)     
  101.         If num_dec = 0 Then dx_str = dx_str + "整"     
  102.         If num_dec > 0 Then dx_str = dx_str + "零" + b + "分"     
  103.     End If     
  104.     If num_dec >= 10 Then     
  105.         a_int = CLng(Mid(str_dec, 1, 1))     
  106.         a = Mid(dx_sz, (a_int + 1), 1)     
  107.         a_int = CLng(Mid(str_dec, 2, 1))     
  108.         b = Mid(dx_sz, (a_int + 1), 1)     
  109.         If a <> "零" Then a = a + "角"     
  110.         If b <> "零" Then b = b + "分" Else b = ""     
  111.         dx_str = dx_str + a + b     
  112.     End If     
  113.              
  114.     dx_str = fu + dx_str     
  115.      
  116.     dx_str = Replace(dx_str, "零亿""亿")     
  117.     dx_str = Replace(dx_str, "零万""万")     
  118.     dx_str = Replace(dx_str, "零千""千")     
  119.     dx_str = Replace(dx_str, "零圆""圆")     
  120.      
  121.      
  122.     CNMoney = dx_str     
  123. End Function    

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

可根据提示进行修正

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

原文地址:http://blog.csdn.net/babyt/archive/2010/10/15/5942285.aspx



RDLC报表中使用自定义函数