1. 程式人生 > >SSRS 實現數字金額轉換為中文大寫漢字

SSRS 實現數字金額轉換為中文大寫漢字

and pre lds 2.0 ber 部分 ima 中文 res

比如:

金額=49,701,120.00,將其轉換為“肆仟玖佰柒拾萬壹仟壹佰貳拾元整”;金額 289,546.52 ,轉換為 “貳拾捌萬玖仟伍佰肆拾陸元伍角貳分”。

技術分享

解決方法:添加VB的轉換函數寫在CODE裏,然後在表達式裏調用自定義函數轉換。

如:增加兩個占位符表達式,如小數位為0時表達式2添加“整”,

表達式1=Code.GetCnString(Fields!Amount.Value)

表達式2=IIF(Fields!Amount.Value)=Fix(Fields!Amount.Value),"整","")

VB轉換函數(轉):

******************貨幣金額大寫轉換庫 Money2String.dll *********************
‘ ‘ ‘中文大寫轉換方法 String GetCnString(String MoneyString)最大轉化金額 999999999999999999999999.99 (24位整數位,2位小數位)方法返回值 轉換成功則返回非零長字串,否則返回零長字串 ‘ ‘英文大寫轉換方法 String GetEnString(String MoneyString)最大轉化金額 999999999999999.99 (15位整數位,2位小數位)方法返回值 轉換成功則返回非零長字串,否則返回零長字串
‘ ‘ ‘程序設計 xlFancy Copyright (C) 2003.03.18 - 2006.12.05郵箱地址 [email protected] ‘ ‘************************************************************************** Option Explicit 中文大寫轉換 Public Function GetCnString(ByVal MoneyString As String) As String Dim i As Long Dim k As Long Dim j As Long Dim
n As Long Dim rmbCapital As String Dim intString As String Dim decString As String Dim tmpString() As String Dim cnNumber() As String Dim cnUnit() As String On Error GoTo cnstr_err: 初始化中文數字、單位 cnNumber = Split("零,壹,貳,叁,肆,伍,陸,柒,捌,玖", ",") cnUnit = Split("分,角,元,拾,佰,仟,萬,拾,佰,仟,億,拾,佰,仟,兆,拾,佰,仟,京,拾,佰,仟,垓,拾,佰,仟", ",") decString = "" intString = MoneyString tmpString = Split(MoneyString, ".") If UBound(tmpString) Then intString = tmpString(0) decString = tmpString(1) End If intString = intString & Left(decString & "00", 2) rmbCapital = "" k = Len(intString) - 1 If k > 25 Then Len(cnUnit) - 1 rmbCapital = MoneyString 超出轉換範圍,返回原值 Else If Val(intString) = 0 Then rmbCapital = "零元" Else For i = 0 To k j = Val(Mid(intString, i + 1, 1)) rmbCapital = rmbCapital & cnNumber(j) & cnUnit(k - i) Next i rmbCapital = Replace(rmbCapital, "零分", "") rmbCapital = Replace(rmbCapital, "零角", "") rmbCapital = Replace(rmbCapital, "零拾", "") rmbCapital = Replace(rmbCapital, "零佰", "") rmbCapital = Replace(rmbCapital, "零仟", "") rmbCapital = Replace(rmbCapital, "零零零", "") rmbCapital = Replace(rmbCapital, "零零", "") rmbCapital = Replace(rmbCapital, "零元", "") rmbCapital = Replace(rmbCapital, "零萬", "") rmbCapital = Replace(rmbCapital, "零億", "") rmbCapital = Replace(rmbCapital, "零兆", "") rmbCapital = Replace(rmbCapital, "零京", "") rmbCapital = Replace(rmbCapital, "零垓", "") rmbCapital = Replace(rmbCapital, "垓京兆億萬", "") rmbCapital = Replace(rmbCapital, "垓京兆億", "") rmbCapital = Replace(rmbCapital, "京兆億萬", "") rmbCapital = Replace(rmbCapital, "垓京兆", "") rmbCapital = Replace(rmbCapital, "京兆億", "") rmbCapital = Replace(rmbCapital, "兆億萬", "") rmbCapital = Replace(rmbCapital, "垓京", "") rmbCapital = Replace(rmbCapital, "垓兆", "") rmbCapital = Replace(rmbCapital, "垓億", "") rmbCapital = Replace(rmbCapital, "垓萬", "") rmbCapital = Replace(rmbCapital, "京兆", "") rmbCapital = Replace(rmbCapital, "京億", "") rmbCapital = Replace(rmbCapital, "京萬", "") rmbCapital = Replace(rmbCapital, "兆億", "") rmbCapital = Replace(rmbCapital, "兆萬", "") rmbCapital = Replace(rmbCapital, "億萬", "") 無小數位時 If Mid(rmbCapital, Len(rmbCapital), 1) = "" Then rmbCapital = Left(rmbCapital, Len(rmbCapital) - 1) End If 無整數位時 If Left(rmbCapital, 1) = "" Then rmbCapital = Mid(rmbCapital, 2, Len(rmbCapital) - 1) End If 無角幣位時 If Left(rmbCapital, 1) = "" Then rmbCapital = Mid(rmbCapital, 2, Len(rmbCapital) - 1) End If End If End If cnstr_exit: GetCnString = rmbCapital Exit Function cnstr_err: rmbCapital = "" 非數字類型錯誤,返回零長字串 Resume cnstr_exit End Function 英文大寫轉換 Public Function GetEnString(ByVal MoneyString As String) As String Dim n As Long Dim k As Long Dim i1 As Long Dim i2 As Long Dim i3 As Long Dim curPoint As Long Dim strbuff1 As String Dim strBuff2 As String Dim strBuff3 As String Dim engCapital As String 保存英文大寫字串 Dim intString As String 保存整數部分字串 Dim decString As String 保存小數部分字串 Dim tmpString() As String Dim enSmallNumber() As String Dim enLargeNumber() As String Dim enUnit() As String On Error GoTo enstr_err 初始化英文數字、單位 enSmallNumber = Split(",ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,ELEVEN,TWELVE,THIRTEEN,FOURTEEN,FIFTEEN,SIXTEEN,SEVENTEEN,EIGHTEEN,NINETEEN", ",") enLargeNumber = Split("TWENTY,THIRTY,FORTY,FIFTY,SIXTY,SEVENTY,EIGHTY,NINETY", ",") enUnit = Split(",THOUSAND,MILLION,BILLION,TRILLION", ",") decString = "" intString = MoneyString tmpString = Split(MoneyString, ".") If UBound(tmpString) Then intString = tmpString(0) decString = tmpString(1) End If decString = Left(decString & "00", 2) engCapital = "" curPoint = Len(intString) If curPoint > 0 And curPoint < 16 Then 以下處理整數部分 k = 0 Do While (curPoint > 0) strbuff1 = "" strBuff2 = "" strBuff3 = "" If curPoint >= 3 Then n = Val(Mid(intString, curPoint - 2, 3)) If n <> 0 Then i1 = Int(n / 100) 取佰位數值 i2 = Int((n - i1 * 100) / 10) 取拾位數值 i3 = n Mod 10 取個位數值 If i1 <> 0 Then strbuff1 = enSmallNumber(i1) + " HUNDRED " End If If i2 <> 0 Then If i2 = 1 Then strBuff2 = enSmallNumber(i2 * 10 + i3) & " " Else strBuff2 = enLargeNumber(i2 - 2) & " " If i3 <> 0 Then strBuff3 = enSmallNumber(i3) & " " End If End If Else If i3 <> 0 Then strBuff3 = enSmallNumber(i3) & " " End If End If engCapital = strbuff1 & strBuff2 & strBuff3 & enUnit(k) & " " & engCapital End If Else n = Val(Left(intString, curPoint)) If n <> 0 Then i2 = Int(n / 10) 取拾位數值 i3 = n Mod 10 取個位數值 If i2 <> 0 Then If i2 = 1 Then strBuff2 = enSmallNumber(i2 * 10 + i3) & " " Else strBuff2 = enLargeNumber(i2 - 2) & " " If i3 <> 0 Then strBuff3 = enSmallNumber(i3) & " " End If End If Else If i3 <> 0 Then strBuff3 = enSmallNumber(i3) & " " End If End If engCapital = strBuff2 & strBuff3 & enUnit(k) & " " & engCapital End If End If k = k + 1 curPoint = curPoint - 3 Loop engCapital = Trim(engCapital) 以下處理小數部分 strBuff2 = "" strBuff3 = "" n = Val(decString) If n <> 0 Then i2 = Int(n / 10) 取拾位數值 i3 = n Mod 10 取個位數值 If i2 <> 0 Then If i2 = 1 Then strBuff2 = enSmallNumber(i2 * 10 + i3) & " " Else strBuff2 = enLargeNumber(i2 - 2) & " " If i3 <> 0 Then strBuff3 = enSmallNumber(i3) & " " End If End If Else If i3 <> 0 Then strBuff3 = enSmallNumber(i3) & " " End If End If 將小數字串追加到整數字串後 If Len(engCapital) > 0 Then engCapital = engCapital & " AND CENTS " & strBuff2 & strBuff3 有整數部分時 Else engCapital = "CENTS " & strBuff2 & strBuff3 只有小數部分時 End If End If engCapital = Trim(engCapital) Else engCapital = MoneyString 超出轉換範圍,返回原值 End If enstr_exit: GetEnString = engCapital Exit Function enstr_err: engCapital = "" 轉換錯誤,返回零長字串 Resume enstr_exit End Function

SSRS 實現數字金額轉換為中文大寫漢字