`

在JasperReport报表中加入大写金额(笔记)

    博客分类:
  • java
阅读更多

摘自 http://www.blogjava.net/hispark/archive/2008/11/19/241321.html

 

我们在制作单证或报表时,客户经常要我们把最后的合计数转写中文大写金额。这个需求很合理,但感觉并不容易实现,如何在JasperReport中加入大写金额的实现呢?提供一种实现的方法给大家参考。

实现思路:
在报表执行过程中使用scirptlet将存放着数字金额的变量读出转换成大写金额字符串后放入大写金额变量中。报表即可象显示普通字符变量一样显示大写金额。

TransChineseMoneyScriptlet.java代码

  1 /** */ /**
  2  * 大写金额转换Scriptlet类
  3  *
  4  *  @author  Spark (Email: spark.unt@gmail.com) 
  5   */

  6 public   class  TransChineseMoneyScriptlet  extends  JRDefaultScriptlet  {
  7      /**/ /*
  8      * 默认构造方法
  9       */

 10      public  TransChineseMoneyScriptlet()  {
 11     
 12     }

 13
 14      /** */ /**
 15      * 获得金额的汉字大写格式 <br>
 16      *  @param  money 小写金额字符串
 17      *  @return  大写的汉字金额
 18       */

 19      public   static  String getChineseMoney(String money)  {
 20         String text  =  transChineseMoney1(money)  +  transChineseMoney2(money);
 21         Pattern p  =  Pattern.compile( " 零分 " , Pattern.CASE_INSENSITIVE);
 22         Matcher m  =  p.matcher(text);
 23         text  =  m.replaceAll( "" );
 24          return  text;
 25     }

 26
 27      /** */ /**
 28      * 截得输入金额的整数部分,并将其转换成中文大写的格式 <br>
 29      * <br>
 30      * 其他描述:输入数字超过接受范围时拒绝转换并退出。<br>
 31      *  @param  传递参数字符串S 参数描述
 32      *  @return  返回转换后的字符串
 33       */

 34      public   static  String transChineseMoney1(String s)  {
 35         String ss  =  s;
 36         String tmpnewchar  =   "" ;
 37         String[] part  =  ss.split( " \\. " );
 38
 39          if  (part[ 0 ].length()  >   10 {
 40              //  超出可转换位数
 41              return   "" ;
 42         }

 43          for  ( int  i  =   0 ; i  <  part[ 0 ].length(); i ++ {
 44              char  perchar  =  part[ 0 ].charAt(i);
 45              if  (perchar  ==   ' 0 ' )
 46                 tmpnewchar  =  tmpnewchar  +   " " ;
 47              if  (perchar  ==   ' 1 ' )
 48                 tmpnewchar  =  tmpnewchar  +   " " ;
 49              if  (perchar  ==   ' 2 ' )
 50                 tmpnewchar  =  tmpnewchar  +   " " ;
 51              if  (perchar  ==   ' 3 ' )
 52                 tmpnewchar  =  tmpnewchar  +   " " ;
 53              if  (perchar  ==   ' 4 ' )
 54                 tmpnewchar  =  tmpnewchar  +   " " ;
 55              if  (perchar  ==   ' 5 ' )
 56                 tmpnewchar  =  tmpnewchar  +   " " ;
 57              if  (perchar  ==   ' 6 ' )
 58                 tmpnewchar  =  tmpnewchar  +   " " ;
 59              if  (perchar  ==   ' 7 ' )
 60                 tmpnewchar  =  tmpnewchar  +   " " ;
 61              if  (perchar  ==   ' 8 ' )
 62                 tmpnewchar  =  tmpnewchar  +   " " ;
 63              if  (perchar  ==   ' 9 ' )
 64                 tmpnewchar  =  tmpnewchar  +   " " ;
 65
 66              int  j  =  part[ 0 ].length()  -  i  -   1 ;
 67              if  (j  ==   0 )
 68                 tmpnewchar  =  tmpnewchar  +   " " ;
 69              if  (j  ==   1   &&  perchar  !=   0 )
 70                 tmpnewchar  =  tmpnewchar  +   " " ;
 71              if  (j  ==   2   &&  perchar  !=   0 )
 72                 tmpnewchar  =  tmpnewchar  +   " " ;
 73              if  (j  ==   3   &&  perchar  !=   0 )
 74                 tmpnewchar  =  tmpnewchar  +   " " ;
 75              if  (j  ==   4   &&  perchar  !=   0 )
 76                 tmpnewchar  =  tmpnewchar  +   " " ;
 77              if  (j  ==   5   &&  perchar  !=   0 )
 78                 tmpnewchar  =  tmpnewchar  +   " " ;
 79              if  (j  ==   6   &&  perchar  !=   0 )
 80                 tmpnewchar  =  tmpnewchar  +   " " ;
 81              if  (j  ==   7   &&  perchar  !=   0 )
 82                 tmpnewchar  =  tmpnewchar  +   " " ;
 83              if  (j  ==   8   &&  perchar  !=   0 )
 84                 tmpnewchar  =  tmpnewchar  +   " 亿 " ;
 85              if  (j  ==   9   &&  perchar  !=   0 )
 86                 tmpnewchar  =  tmpnewchar  +   " " ;
 87         }

 88          return  tmpnewchar;
 89     }

 90
 91      /** */ /**
 92      * 截得输入金额的小数部分,并将其转换成中文大写的格式 <br>
 93      * <br>
 94      * 其他描述:小数部分超过两位时系统自动截断。<br>
 95      * 
 96      *  @param  传递参数字符串
 97      * 
 98      *  @return  返回转换后的字符串
 99       */

100      public   static  String transChineseMoney2(String s)  {
101         String ss  =  s;
102         String tmpnewchar1  =   "" ;
103         String[] part  =  ss.split( " \\. " );
104
105          if  (ss.indexOf( " . " !=   - 1 {
106              if  (part[ 1 ].length()  >   2 {
107                  //  MessageDialog.openInformation(null,"提示","小数点之后只能保留两位,系统将自动截段");
108                 part[ 1 =  part[ 1 ].substring( 0 2 );
109             }

110              for  ( int  i  =   0 ; i  <  part[ 1 ].length(); i ++ {
111                  char  perchar  =  part[ 1 ].charAt(i);
112 //                 System.out.println(perchar);
113                  if  (perchar  ==   ' 0 ' )
114                     tmpnewchar1  =  tmpnewchar1  +   " " ;
115                  if  (perchar  ==   ' 1 ' )
116                     tmpnewchar1  =  tmpnewchar1  +   " " ;
117                  if  (perchar  ==   ' 2 ' )
118                     tmpnewchar1  =  tmpnewchar1  +   " " ;
119                  if  (perchar  ==   ' 3 ' )
120                     tmpnewchar1  =  tmpnewchar1  +   " " ;
121                  if  (perchar  ==   ' 4 ' )
122                     tmpnewchar1  =  tmpnewchar1  +   " " ;
123                  if  (perchar  ==   ' 5 ' )
124                     tmpnewchar1  =  tmpnewchar1  +   " " ;
125                  if  (perchar  ==   ' 6 ' )
126                     tmpnewchar1  =  tmpnewchar1  +   " " ;
127                  if  (perchar  ==   ' 7 ' )
128                     tmpnewchar1  =  tmpnewchar1  +   " " ;
129                  if  (perchar  ==   ' 8 ' )
130                     tmpnewchar1  =  tmpnewchar1  +   " <span styl
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics