图示ireport 中使用javabean 作数据源开发 基于 jasperreports 报表 过程
——学习笔记系列之ireport起步
xmlin
本文不讲原理,因为网上的资源很多,本文以一个简单的日销售报表为例,记录在ireport中使用javabean 作数据源开发 基于 jasperreports 报表 过程 .
一. 准备工作
1.正确安装jdk
2.到
http://ireport.sourceforge.net/ 去下载ireport,本文使用1.3.1版本,解压iReport在任意目录,解压后的文件里面有一个iReport.bat,通过双击运行。
3.到
http://jasperreports.sourceforge.net/index.html 下载jasperreport,本文使用的是1.3.1版本,解压到任意目录,开发中需要使用其中的
jasperreports-1.3.1.jar , commons-logging-1.0.2.jar , commons-collections-2.1.jar.
二.创建javabean
1.创建DailySales.java,一个简单VO bean。
import java.io.Serializable;
public class DailySales implements Serializable
{
private static final long serialVersionUID = 1L;
private String productNo ;
private String productName ;
private int number ;
private int money ;
private int id ;
public DailySales(String productNo, String productName, int number, int money)
{
this . productNo = productNo;
this . productName = productName;
this . number = number;
this . money = money;
}
public String getProductNo()
{
return productNo ;
}
public void setProductNo(String productNo)
{
this . productNo = productNo;
}
public String getProductName()
{
return productName ;
}
public void setProductName(String productName)
{
this . productName = productName;
}
public int getNumber()
{
return number ;
}
public void setNumber( int number)
{
this . number = number;
}
public int getMoney()
{
return money ;
}
public void setMoney( int money)
{
this . money = money;
}
public int getId()
{
return id ;
}
public void setId( int id)
{
this . id = id;
}
}
2. 创建 DailySalesDataSource.java, 这是报表的数据源。这个类实现了 jasperreports 中提供的数据源接口 JRDataSource, 实现其中的两个方法 :next() 和 getFieldValue(JRField field) 。
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
public class DailySalesDataSource implements JRDataSource
{
/**
* 测试数据,实际项目中是动态获取,也不一定是数组,可以是其它的数据类型 .
*/
private Object[][] data =
{
{ " 货号 1" , " 物品1 " , 1,1000},
{ " 货号 2" , " 物品 2" , 2,2000},
{ " 货号 3" , " 物品 3" , 3,3000},
{ " 货号 4" , " 物品 4" , 4,4000},
{ " 货号 5" , " 物品 5" , 5,5000},
{ " 货号 6" , " 物品 6" , 6,6000},
{ " 货号 7" , " 物品 7" , 7,7000},
{ " 货号 8" , " 物品 8" , 8,8000},
{ " 货号 9" , " 物品 9" , 9,9000},
{ " 货号 10" , " 物品 10" , 10,10000}
};
private int index = -1;
public DailySalesDataSource()
{
}
/**
* 实现了 JRDataSource 中的方法.判断是否还有下一个.
*/
public boolean next() throws JRException
{
index ++;
return ( index < data . length );
}
/**
* 实现了 JRDataSource 中的方法.
* @param field 是对应报表中的要填充的字段的名称.
*/
public Object getFieldValue(JRField field ) throws JRException
{
Object value = null ;
String fieldName = field .getName();
if ( "id" .equals(fieldName))
{
value = index+1 ;
}
else if ( "productNo" .equals(fieldName))
{
value = data [ index ][0];
}
else if ( "productName" .equals(fieldName))
{
value = data [ index ][1];
}
else if ( "number" .equals(fieldName))
{
value = data [ index ][2];
}
else if ( "money" .equals(fieldName))
{
value = data [ index ][3];
}
return value;
}
}
3 .在 ireport 中使用,取得测试数据源 , 如果不使用 ireport 工具,则只需要上面的两个类。
import java.util.Arrays;
import java.util.Collection;
/**
* 简单工厂类,取得测试数据
* @author xmlin
*
*/
public class DailySalesFactory
{
private static DailySales[] data =
{
new DailySales( " 货号 1" , " 物品1 " , 1,1000),
new DailySales( " 货号 2" , " 物品 2" , 2,2000),
new DailySales( " 货号 3" , " 物品 3" , 3,3000),
new DailySales( " 货号 4" , " 物品 4" , 4,4000),
new DailySales( " 货号 5" , " 物品 5" , 5,5000),
new DailySales( " 货号 6" , " 物品 6" , 6,6000),
new DailySales( " 货号 7" , " 物品 7" , 7,7000),
new DailySales( " 货号 8" , " 物品 8" , 8,8000),
new DailySales( " 货号 9" , " 物品 9" , 9,9000),
new DailySales( " 货号 10" , " 物品 10" , 10,10000)
};
public static Object[] getBeanArray()
{
return data ;
}
public static Collection getBeanCollection()
{
return Arrays.asList ( data );
}
}
三.使用ireport开发报表样式
1.新建一个项目。
2.设置类路径,在菜单“options”中选择Classpath,点击在弹出框中的add folder,填写javabean编译成的.class文件存放的路径. 点save Classpath完成。如图
3.设置数据源.在菜单"Data"中选择”Connection/Data sources”, 点击在弹出框中的new按钮增加一个数据源.如图
其中Name随便取一个名字,Type of Connection/Data 选择 JavaBeans set data source,如果使用其它的数据源则选择其它的选项.Factory class 为我们刚才创建的Factory类,里面包含取得测试数据的静态方法getBeanCollection().用Test测试是否成功,点Save保存.
4. 设置活动连接.在菜单"Data"中选择”Set Active Connection”.
5.Report Query , 在菜单"Data"中选择” Report Query”,填写javabean,即我们创建的VO bean.如图
6.设计报表.
设计日期字段如图
设计填充字段,如图
设计页数字段如图
设计好的报表样式如图
点菜单"build"中的"compile"进行编译,然后再”execute with connection datasource”就可以看到报表的结果了.
报表设计完成,在ireport的执行路径下会生成一个DailySales.jasper的文件.
四.编写测试类.
生成的DailySales.jasper可以在web服务端生成报表,也可以在肥客户端如swing生成,这里写一个在肥客户端和简单运用.
import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
public class TestReport
{
public static void main(String[] args)
{
TestReport.showReport ();
}
private static void showReport()
{
String reportPath = "D:\\dailySales.jasper" ;
Map parameters = new HashMap();
// 如果报表中有用到变量,在这里给它赋值.
//parameters.put("ReportTitle", " 报表标题 ");
try
{
JasperReport jasperReport = (JasperReport) JRLoader.loadObject (reportPath);
JasperPrint jasperPrint = JasperFillManager.fillReport (jasperReport, parameters, new DailySalesDataSource());
JasperViewer jrview = new JasperViewer(jasperPrint);
jrview.setPreferredSize( new Dimension(200,100));
jrview.setVisible( true );
}
catch (JRException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}