我們在制作單證或報表時,客戶經常要我們把最后的合計數轉寫中文大寫金額。這個需求很合理,但感覺并不容易實現,如何在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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

后面幾個方法都是iReport所需的幾個方法,不要刪除掉它。
然后在報表中將定義兩個報表變量:
sumTaxMoney 用于存放小寫金額,它是Double型,并在iReport中寫上計算公式或script
cnMoney 用于接收本scriptlet傳回的大寫金額變量,在iReport中無需賦值,直接放到需顯示的地方即可
用紅框框起來的部分就是我們想要的結果,是不是很酷呀?
差點忘記了關鍵的啦,要在你的報表XML中jasperReport節點中增加以下屬性值scriptletClass="TransChineseMoneyScriptlet" ,來啟用該scriptlet