我们在制作单证或报表时,客户经常要我们把最后的合计数转写中文大写金额。这个需求很合理,但感觉并不容易实现,如何在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 + "捌";
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! */
145
public 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! */
151
public 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! */
164
public 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! */
170
public 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! */
176
public 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! */
184
public 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! */
190
public 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! */
198
public 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! */
204
public 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! */
210
public 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中无需赋值,直接放到需显示的地方即可
用红框框起来的部分就是我们想要的结果,是不是很酷呀?
差点忘记了关键的啦,要在你的报表XML中jasperReport节点中增加以下属性值scriptletClass="TransChineseMoneyScriptlet" ,来启用该scriptlet