iReport+jasperReport 之scriptlet

提起scriptlet就不能不联想到它的强大功能,jasperReport也是支持scriptlet的哦,先分析一下JasperReport的 API吧!
在填充报表时scriplet是一个非常有力的工具,JRAbstractScriptlet.java位于 net.sf.jasperreports.engine包下是一个抽象



beforeReportInit() ,afterReportInit() ,beforePageInit(),afterPageInit(), beforeColumnInit(), afterColumnInit() ,beforeGroupInit(String groupName),afterGroupInit(String groupName) 
看看这些名字就知道你能完成那些功能,这几个方法是要求我们实现的,jasperReport给我们提供了一个实现类 JRDefaultScriptlet.java,默认的空实现了上面几个方法,它只是很便利的为我们提供了所需的八个方法的空实现。我们写自己的 scriptlet时需要继承JRDefaultScriptlet.java这个类实现自己的相应的功能即可。
 先来看一个简单的例子:
先看看模板文件的处理:
新建时填写的这个类是下面我们要介绍的继承自JRDefaultScriptlet.java类,也就是在模板文件中我们要加上如下代码

scriptletClass="org.bulktree.ireport.scriptlet.ScriptletReportDemo"

完整的模板文件如下:scriptletDemo.jrxml

<? xml version="1.0" encoding="UTF-8"   ?>
<!--  Created with iReport - A designer for JasperReports  -->
<! DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd" >
< jasperReport
         
name ="scriptletDemo"
         columnCount
="1"
         printOrder
="Vertical"
         orientation
="Portrait"
         pageWidth
="595"
         pageHeight
="842"
         columnWidth
="535"
         columnSpacing
="0"
         leftMargin
="30"
         rightMargin
="30"
         topMargin
="20"
         bottomMargin
="20"
         whenNoDataType
="NoPages"
         scriptletClass
="org.bulktree.ireport.scriptlet.ScriptletReportDemo"
         isTitleNewPage
="false"
         isSummaryNewPage
="false" >
    
< property  name ="ireport.scriptlethandling"  value ="2"   />
    
< property  name ="ireport.encoding"  value ="UTF-8"   />
    
< import  value ="java.util.*"   />
    
< import  value ="net.sf.jasperreports.engine.*"   />
    
< import  value ="net.sf.jasperreports.engine.data.*"   />

    
< parameter  name ="ReportTitle"  isForPrompting ="true"  class ="java.lang.String" />

        
< background >
            
< band  height ="0"   isSplitAllowed ="true"   >
            
</ band >
        
</ background >
        
< title >
            
< band  height ="20"   isSplitAllowed ="true"   >
                
< textField  isStretchWithOverflow ="false"  isBlankWhenNull ="false"  evaluationTime ="Now"  hyperlinkType ="None"   hyperlinkTarget ="Self"   >
                    
< reportElement
                        
mode ="Opaque"
                        x
="193"
                        y
="0"
                        width
="134"
                        height
="18"
                        backcolor
="#FFCC33"
                        key
="textField" />
                    
< box ></ box >
                    
< textElement  textAlignment ="Center"  verticalAlignment ="Middle" >
                        
< font  pdfFontName ="Helvetica-Bold"  size ="12"  isBold ="true" />
                    
</ textElement >
                
< textFieldExpression    class ="java.lang.String" > <![CDATA[ $P{ReportTitle} ]]> </ textFieldExpression >
                
</ textField >
            
</ band >
        
</ title >
        
< pageHeader >
            
< band  height ="0"   isSplitAllowed ="true"   >
            
</ band >
        
</ pageHeader >
        
< columnHeader >
            
< band  height ="0"   isSplitAllowed ="true"   >
            
</ band >
        
</ columnHeader >
        
< detail >
            
< band  height ="264"   isSplitAllowed ="true"   >
                
< textField  isStretchWithOverflow ="false"  isBlankWhenNull ="false"  evaluationTime ="Now"  hyperlinkType ="None"   hyperlinkTarget ="Self"   >
                    
< reportElement
                        
x ="85"
                        y
="20"
                        width
="329"
                        height
="61"
                        forecolor
="#FF0099"
                        key
="textField-1" />
                    
< box ></ box >
                    
< textElement  textAlignment ="Center"  verticalAlignment ="Middle" >
                        
< font  pdfFontName ="Helvetica-Bold"  size ="20"  isBold ="true" />
                    
</ textElement >
                
< textFieldExpression    class ="java.lang.String" > <![CDATA[ $P{REPORT_SCRIPTLET}.showInfor() ]]> </ textFieldExpression >
                
</ textField >
            
</ band >
        
</ detail >
        
< columnFooter >
            
< band  height ="0"   isSplitAllowed ="true"   >
            
</ band >
        
</ columnFooter >
        
< pageFooter >
            
< band  height ="0"   isSplitAllowed ="true"   >
            
</ band >
        
</ pageFooter >
        
< summary >
            
< band  height ="50"   isSplitAllowed ="true"   >
            
</ band >
        
</ summary >
</ jasperReport >

下来看看怎么实现我们自己的scriplet

ScriptletReportDemo.java

package  org.bulktree.ireport.scriptlet;

import  net.sf.jasperreports.engine.JRDefaultScriptlet;
import  net.sf.jasperreports.engine.JRScriptletException;

/**
 * 
 * 
@author  bulktree Email: laoshulin@gmail.com
 * @Nov 26, 2008
 
*/

public   class  ScriptletReportDemo  extends  JRDefaultScriptlet  {

    @Override
    
public   void  afterColumnInit()  throws  JRScriptletException  {
        System.out.println(
" **************************************afterColumnInit()************************************** " );
    }


    @Override
    
public   void  afterDetailEval()  throws  JRScriptletException  {
        System.out.println(
" **************************************afterDetailEval()************************************** " );
    }


    @Override
    
public   void  afterGroupInit(String groupName)  throws  JRScriptletException  {
        System.out.println(
" **************************************afterDetailEval()************************************** " );
    }


    @Override
    
public   void  afterPageInit()  throws  JRScriptletException  {
        System.out.println(
" **************************************afterPageInit()************************************** " );
    }


    @Override
    
public   void  afterReportInit()  throws  JRScriptletException  {
        System.out.println(
" **************************************afterReportInit() begin************************************** " );
        
        String str 
=  (String)  this .getParameterValue( " ReportTitle " );
        System.out.println(
" report title=====>>>> " + str);
        
        str 
+=  str.subSequence( 0 , str.length() - 2 );
        
this .setParameterValue( " ReportTitle " , str);
        System.out.println(
" **************************************afterReportInit() end************************************** " );
    }


    @Override
    
public   void  beforeColumnInit()  throws  JRScriptletException  {
        System.out.println(
" **************************************beforeColumnInit()************************************** " );
    }


    @Override
    
public   void  beforeDetailEval()  throws  JRScriptletException  {
        System.out.println(
" **************************************beforeDetailEval()************************************** " );
    }


    @Override
    
public   void  beforeGroupInit(String groupName)  throws  JRScriptletException  {
        System.out.println(
" **************************************beforeGroupInit()************************************** " );
    }


    @Override
    
public   void  beforePageInit()  throws  JRScriptletException  {
        System.out.println(
" **************************************beforePageInit()************************************** " );
    }


    @Override
    
public   void  beforeReportInit()  throws  JRScriptletException  {
        System.out.println(
" **************************************beforeReportInit()************************************** " );
    }


    
public  String showInfor()  throws  JRScriptletException  {
        
return   " the is scriptlet scriptlet scriptlet the,sscriptlet report the is ascriptlet report this is a scriptlet report this is a scriptlet report " ;
    }

    
}

这段代码最后一个方法是我们自己的加的,用来在报表上显示一段文本。我们知道对于一个Field、Parameter、 Variable,JasperReport分别采 用$F{FieldName}、$P{Parametername}、$V{VariableName}来引用,而如果要引用 ScriptletReportDemo.java类的showInfor()返回字符串显示在报表上,看看这个就知道了

在afterReportInit方法中我们把parameter字段取出来前后添上五个*号再set进去
    下来写一个test类测试一下:

package  org.bulktree.ireport.scriptlet;

import  java.io.File;
import  java.io.FileInputStream;
import  java.io.InputStream;
import  java.util.HashMap;

import  net.sf.jasperreports.engine.JREmptyDataSource;
import  net.sf.jasperreports.engine.JasperCompileManager;
import  net.sf.jasperreports.engine.JasperFillManager;
import  net.sf.jasperreports.engine.JasperPrint;
import  net.sf.jasperreports.engine.JasperReport;
import  net.sf.jasperreports.view.JasperViewer;

/**
 * 
 * 
@author  bulktree Email: laoshulin@gmail.com
 * @Nov 27, 2008
 
*/

public   class  ScriptletTestMain  {

    
public   static   void  main(String[] args)  {
        String path 
=   " D:/workspace/scriptletDemo.jrxml " ;

        File file 
=   new  File(path);
        InputStream in;
        
try   {

            HashMap parameters 
=   new  HashMap();
            parameters.put(
" ReportTitle " " LAOSHULIN " );
            in 
=   new  FileInputStream(file);
            JasperReport jasperReport 
=  JasperCompileManager.compileReport(in);
            JasperPrint jasperPrint 
=  JasperFillManager.fillReport(jasperReport,
                    parameters, 
new  JREmptyDataSource());
            JasperViewer viewer 
=   new  JasperViewer(jasperPrint);
            viewer.setVisible(
true );

        }
  catch  (Exception e)  {
            
//  TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}


效果不错吧:

 

 

http://www.blogjava.net/bulktree/archive/2008/12/17/246786.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值