ireport本身没有提供这个方法,但因为ireport完全使用java编写的,所有对java的支持非常好,我们可以利用这一点完美解决。
首先新建一个TransChineseMoneyScriptlet.java继承JRAbstractScriptlet类,具体方法number2CNMontrayUnit实现金额小写转大写的功能,代码如下:(这是本人从网上找的一段金额小写转大写的代码)
第一步:
public class TransChineseMoneyScriptlet extends JRAbstractScriptlet{
private static final String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆",
"伍", "陆", "柒", "捌", "玖" };
private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元",
"拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
"佰", "仟" };
private static final String CN_FULL = "整";
private static final String CN_NEGATIVE = "负";
private static final int MONEY_PRECISION = 2;
private static final String CN_ZEOR_FULL = "零元整";
public static String number2CNMontrayUnit(Double numberOfMoney){
StringBuffer sb = new StringBuffer();
BigDecimal cnMoney = new BigDecimal(String.valueOf(numberOfMoney));
int signum = cnMoney.signum();
if (signum == 0) {
return "零元整";
}
long number = cnMoney.movePointRight(2)
.setScale(0, 4).abs().longValue();
long scale = number % 100L;
int numUnit = 0;
int numIndex = 0;
boolean getZero = false;
if (scale <= 0L) {
numIndex = 2;
number /= 100L;
getZero = true;
}
if ((scale > 0L) && (scale % 10L <= 0L)) {
numIndex = 1;
number /= 10L;
getZero = true;
}
int zeroSize = 0;
while (number > 0L){
numUnit = (int)(number % 10L);
if (numUnit > 0) {
if ((numIndex == 9) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
}
if ((numIndex == 13) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
}
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
getZero = false;
zeroSize = 0;
} else {
zeroSize++;
if (!getZero) {
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
}
if (numIndex == 2) {
if (number > 0L)
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
else if (((numIndex - 2) % 4 == 0) && (number % 1000L > 0L)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
getZero = true;
}
number /= 10L;
numIndex++;
}
if (signum == -1) {
sb.insert(0, "负");
}
if (scale <= 0L) {
sb.append("整");
}
return sb.toString();
}
public static void main(String[] args) {
double money = 2020004.01D;
String s = number2CNMontrayUnit(Double.valueOf(money));
System.out.println("===");
System.out.println("你输入的金额为:【" + money + "】 #--# [" + s.toString() + "]");
}
public void afterColumnInit()
throws JRScriptletException
{
}
public void afterDetailEval()
throws JRScriptletException
{
}
public void afterGroupInit(String arg0)
throws JRScriptletException
{
}
public void afterPageInit()
throws JRScriptletException
{
}
public void afterReportInit()
throws JRScriptletException
{
}
public void beforeColumnInit()
throws JRScriptletException
{
}
public void beforeDetailEval()
throws JRScriptletException
{
}
public void beforeGroupInit(String arg0)
throws JRScriptletException
{
}
public void beforePageInit()
throws JRScriptletException
{
}
public void beforeReportInit()
throws JRScriptletException
{
}
}
需要引入ireport的jar包,直接在pom.xml加入以下代码
net.sf.jasperreports
jasperreports
6.5.1
com.itextpdf
itext-asian
5.1.0
com.itextpdf
itextpdf
5.5.6
com.lowagie
itext
4.2.0
第二步:把这个类打成jar包或者直接用编译后的.class文件引入到ireport中(使用jdk1.7及以下的)。
第三步:在ireport自己的模板中,选中Scriptlets下的REPORT(Scriptlets通常在Variables下面),在右边REPORT - 属性 中的Scriptlet Class后面,写上类的全路径(包名+类名),如(org.test.h.TransChineseMoneyScriptlet),不用修改上面的Name属性
第四步:新加一个Text Field到模板界面,选中Text Feild框,然后在右边的属性中,设置Text field properties下的Text field Expression的值,格式为((脚本的包名+类名)$P{REPORT_SCRIPTLET}).number2CNMontrayUnit(要转化的字段或变量),然后将Expression Class属性,设为String,其他属性不用修改
例如:((org.test.h.TransChineseMoneyScriptlet)$P{REPORT_SCRIPTLET}).number2CNMontrayUnit($F{EBCU_ID}),EBCU_ID字段是金额的小写值
查看结果: