继上篇bean数据源,如果我们自己定义自己的数据源该如何去写呢?
jasperReport提供了很多的便利去实现自己的DataSource,简单的有三种方式:
直接实现bean的方式 bean工厂 表格模型
还是使用前面的person.jasper文件,和Person.java VO类
此种方式需要实现JRDataSource接口,定义一个二维对象数组用来存放数据,通过遍历数组的数据实现getFieldValue()和next()方法
PersonDataSource.java
package
org.bulktree.ireport.customdata;
import
javax.activation.DataSource;
import
net.sf.jasperreports.engine.JRDataSource;
import
net.sf.jasperreports.engine.JRException;
import
net.sf.jasperreports.engine.JRField;

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

public
class
PersonDataSource
implements
JRDataSource
{

public PersonDataSource()
{
}

private Object[][] data =
{
{ "001", "bulktree1", "Man1", "21", "001111", "IsoftStone1" },
{ "002", "bulktree2", "Man2", "22", "002222", "IsoftStone2" },
{ "003", "bulktree3", "Man3", "23", "003333", "IsoftStone3" },
{ "004", "bulktree4", "Man4", "24", "004444", "IsoftStone4" },
{ "005", "bulktree5", "Man5", "25", "005555", "IsoftStone5" },
{ "006", "bulktree6", "Man6", "26", "006666", "IsoftStone6" },
{ "007", "bulktree7", "Man7", "27", "007777", "IsoftStone7" },
{ "008", "bulktree8", "Man8", "28", "008888", "IsoftStone8" },
{ "009", "bulktree9", "Man9", "29", "009999", "IsoftStone9" },
{ "001", "oakertree1", "Man1", "21", "001111", "IsoftStone1" },
{ "002", "oakertree2", "Man2", "22", "002222", "IsoftStone2" },
{ "003", "oakertree3", "Man3", "23", "003333", "IsoftStone3" },
{ "004", "oakertree4", "Man4", "24", "004444", "IsoftStone4" },
{ "005", "oakertree5", "Man5", "25", "005555", "IsoftStone5" },
{ "006", "oakertree6", "Man6", "26", "006666", "IsoftStone6" },
{ "007", "oakertree7", "Man7", "27", "007777", "IsoftStone7" },
{ "008", "oakertree8", "Man8", "28", "008888", "IsoftStone8" },
{ "009", "oakertree9", "Man9", "29", "009999", "IsoftStone9" },
{ "001", "laoshulin1", "Man1", "21", "001111", "IsoftStone1" },
{ "002", "laoshulin2", "Man2", "22", "002222", "IsoftStone2" },
{ "003", "laoshulin3", "Man3", "23", "003333", "IsoftStone3" },
{ "004", "laoshulin4", "Man4", "24", "004444", "IsoftStone4" },
{ "005", "laoshulin5", "Man5", "25", "005555", "IsoftStone5" },
{ "006", "laoshulin6", "Man6", "26", "006666", "IsoftStone6" },
{ "007", "laoshulin7", "Man7", "27", "007777", "IsoftStone7" },
{ "008", "laoshulin8", "Man8", "28", "008888", "IsoftStone8" },
{ "009", "laoshulin9", "Man9", "29", "009999", "IsoftStone9" } };
private int index = -1;

public Object getFieldValue(JRField jrField) throws JRException
{
Object value = null;
String fieldName = jrField.getName();

if ("pid".equals(fieldName))
{
value = data[index][0];
}
if ("name".equals(fieldName))
{
value = data[index][1];
}
if ("sex".equals(fieldName))
{
value = data[index][2];
}
if ("age".equals(fieldName))
{
value = data[index][3];
}
if ("password".equals(fieldName))
{
value = data[index][4];
}
if ("department".equals(fieldName))
{
value = data[index][5];
}
return value;
}

本文介绍如何为JasperReports自定义数据源,通过实现JRDataSource接口并利用二维数组存储数据,实现getFieldValue()和next()方法来提供报表所需数据。
236

被折叠的 条评论
为什么被折叠?



