spark的自留地(ofbiz/eclipse rcp/shark/opentaps)

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  54 Posts :: 0 Stories :: 112 Comments :: 0 Trackbacks

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

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


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

  6public 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(02);
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 + "";
131                if (perchar == '9')
132                    tmpnewchar1 = tmpnewchar1 + "";
133
134                if (i == 0 && perchar != 0)
135                    tmpnewchar1 = tmpnewchar1 + "";
136                if (i == 1 && perchar != 0)
137                    tmpnewchar1 = tmpnewchar1 + "";
138            }

139        }

140        return tmpnewchar1;
141    }

142
143
144/** Begin EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */
145public void afterColumnInit() throws JRScriptletException
146{
147    super.beforeColumnInit();
148}

149/** End EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */
150/** Begin EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */
151public void afterDetailEval() throws JRScriptletException
152{
153    Double sumTaxMoney = getVariableValue("sumTaxMoney"== null ? new Double(0.0)
154    : (java.lang.Double) getVariableValue("sumTaxMoney");
155
156//    System.out.println("sumTaxMoney = " + sumTaxMoney);
157    String cnMoney = getChineseMoney(sumTaxMoney+"");
158//    System.out.println("cnMoney = " + cnMoney);
159    this.setVariableValue("cnMoney", cnMoney);
160    super.afterDetailEval();
161}

162/** End EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */
163/** Begin EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't modify or move please! */
164public void afterGroupInit(String groupName) throws JRScriptletException
165{
166    super.afterGroupInit(groupName);
167}

168/** End EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't modify or move please! */
169/** Begin EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify or move please! */
170public void afterPageInit() throws JRScriptletException
171{
172    super.afterPageInit();
173}

174/** End EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify or move please! */
175/** Begin EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't modify or move please! */
176public void afterReportInit() throws JRScriptletException
177{
178    
179    
180    
181}

182/** End EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't modify or move please! */
183/** Begin EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */
184public void beforeColumnInit() throws JRScriptletException
185{
186        
187}

188/** End EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */
189/** Begin EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */
190public void beforeDetailEval() throws JRScriptletException
191{
192    
193}

194/** end EVENT_BEFORE_DETAIL_EVAL Please don't touch or move this comment*/
195
196/** End EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */
197/** Begin EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't modify or move please! */
198public void beforeGroupInit(String groupName) throws JRScriptletException
199{
200    
201}

202/** End EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't modify or move please! */
203/** Begin EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't modify or move please! */
204public void beforePageInit() throws JRScriptletException
205{
206    
207}

208/** End EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't modify or move please! */
209/** Begin EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't modify or move please! */
210public void beforeReportInit() throws JRScriptletException
211{
212    
213}

214
215/** End EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't modify or move please! */
216
217}

后面几个方法都是iReport所需的几个方法,不要删除掉它。

然后在报表中将定义两个报表变量:
sumTaxMoney  用于存放小写金额,它是Double型,并在iReport中写上计算公式或script
cnMoney 用于接收本scriptlet传回的大写金额变量,在iReport中无需赋值,直接放到需显示的地方即可
jasperreport单据报表

用红框框起来的部分就是我们想要的结果,是不是很酷呀?

差点忘记了关键的啦,要在你的报表XML中jasperReport节点中增加以下属性值scriptletClass="TransChineseMoneyScriptlet" ,来启用该scriptlet
posted on 2008-11-19 12:20 shanghai_spark 阅读(4010) 评论(13)  编辑  收藏 所属分类: pentaho & jasperreport

Feedback

# re: 在JasperReport报表中加入大写金额 2008-11-19 15:38 凌晨风
哥们!有研究过纯Java代码做一个crosstab吗?我现在在做这个接口,能交流下吗?  回复  更多评论
  

# re: 在JasperReport报表中加入大写金额 2008-11-19 19:37 广州spark
@凌晨风
jasper report或是别的报表引擎如birt做个crosstab是很容易的呀,它们都是纯java的实现,你要重新发明轮子吗?  回复  更多评论
  

# re: 在JasperReport报表中加入大写金额 2008-11-19 22:02 扭曲的铅笔
你好,你现在用JasperReport和ireport的版本是多少?
想向您学习学习。  回复  更多评论
  

# re: 在JasperReport报表中加入大写金额 2008-11-19 22:21 广州spark
@扭曲的铅笔
说起来很惭愧,最近三年都没更新过版本了,一直在用1.2.5,真的没觉得有什么不够用. 而且一个东西用久了有一个好处,连它的一些bug你都知道甚至喜欢在哪里了.如果你是初学者的话,当然用最新的也没问题,不过不要用nb版.那个玩意对中文支持不好. 做报表是一个细致的活,格式与复杂的查询都很折磨人,锻炼性情非常棒!  回复  更多评论
  

# re: 在JasperReport报表中加入大写金额 2008-11-20 10:34 凌晨风
我们这边需要提供给别人接口,而Ireport做的交叉报表功能有限啊!  回复  更多评论
  

# re: 在JasperReport报表中加入大写金额 2008-11-20 18:57 广州spark
@凌晨风
或许你可以谈下你的需求,我想如果iReport做不到,再考虑自己实现,但实际上我们往往会以后它们没想过.这种傻事我干过N次了.所以现在做什么都要先找下有无更好的解决方案.  回复  更多评论
  

# re: 在JasperReport报表中加入大写金额 2008-11-21 14:00 凌晨风
我们这边有自己的报表系统,集成了好多优秀的报表,现在对jasperReport的支持不是很好,有时间私聊吧!laoshulin@gmail.com  回复  更多评论
  

# re: 在JasperReport报表中加入大写金额 2009-06-20 17:42 leoyang
你好,看了您的文章,我试了一下,没有成功,不知道是哪儿出了问题,变量cnMoney没有出现大写金额,是不是还需要设置其他的地方,这是我的邮箱:leoyang0123@126.com 请教您能否给解决一下  回复  更多评论
  

# re: 在JasperReport报表中加入大写金额[未登录] 2012-06-05 17:23 charles
呵呵,我最近用开发用到birt,但是ofbiz里面birt有问题,或者说,目前没调通。我想,我们应该成为朋友。扣扣:一零五三七二二九零  回复  更多评论
  

# re: 在JasperReport报表中加入大写金额 2013-04-24 10:22
啊  回复  更多评论
  

# re: 在JasperReport报表中加入大写金额[未登录] 2013-04-24 10:23
您好,看了您的帖,还是没能实现效果,那个写的java 类是怎么引导irpeoret 里面去的??????  回复  更多评论
  

# re: 在JasperReport报表中加入大写金额 2013-05-04 10:01 搜索
您好,您写的类有bug , 如38707.20,大写出来,有问题  回复  更多评论
  

# re: 在JasperReport报表中加入大写金额 2013-05-04 17:55 搜索
您的帖子,有很多的dug ,如当 金额为整数时,1000 ,为壹仟零佰零零元零角,这样的  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: