JasperFillManager.fillReport()這個方法在使用JDBC數據源時采用一個打開的數據庫連接(getConn),除此之外jasperReport給我們提供了一個JRDataSource接口,用以實現我們自己的數據源
JRDataSource接口只有兩個方法
public interface JRDataSource
{
/**
* 針對當前的數據源返回游標指向的下一個元素的值,
*
*/
public boolean next() throws JRException;
/**
* 返回游標指向的當前值
*
*/
public Object getFieldValue(JRField jrField) throws JRException;
}
JRBeanCollectionDataSource
此種方式是最簡單的一種,查看API我們就可以發現,JRBeanCollectionDataSource繼承JRAbstractBeanDataSource類,而JRAbstractBeanDataSource是一個抽象類它間接的實現了JRDataSource這個接口,所以我們就可以不用自己去實現next()/getFieldValue()這兩個方法了。
看到JRBeanCollectionDataSource這個類名大概就知道怎么用了吧!
我們的模板文件還是上篇的
JDBC數據源的模板,只是沒有了查詢語句,手工建立所需的幾個字段(域)
新建一vo對象Person.java,最基本的getter、setter方法
package org.bulktree.ireport.customdata;


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

public class Person
{
private String pid;
private String name;
private String sex;
private String age;
private String password;
private String department;

public Person(String pid, String name, String sex, String age, String password,

String department)
{
this.pid = pid;
this.name = name;
this.sex = sex;
this.age = age;
this.password = password;
this.department = department;
}


public Person()
{

}

}

下來準備數據
Person p1 = new Person();
p1.setAge("23");
p1.setDepartment("ISoftStone");
p1.setName("LAOSHULIN");
p1.setPassword("123456789");
p1.setPid("2008040516058772hj");
p1.setSex("man");
既然它是一個BeanCollection我們 就來一個從conllection吧!
List<Person> list = new ArrayList<Person>();
list.add(p1);
查看API,JRBeanCollectionDataSource的構造函數
public JRBeanCollectionDataSource(Collection beanCollection)

{
this(beanCollection, true);
}

/** *//**
*
*/
public JRBeanCollectionDataSource(Collection beanCollection, boolean isUseFieldDescription)

{
super(isUseFieldDescription);
this.data = beanCollection;

if (this.data != null)

{
this.iterator = this.data.iterator();
}
}
看看描述我們使用第一個
JRDataSource datesource = new JRBeanCollectionDataSource(list);
這個數據源就構造完畢了,很簡單吧!這樣報表就可以預覽了,完整的代碼如下:
package org.bulktree.ireport.customdata;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JRViewer;
import net.sf.jasperreports.view.JasperViewer;


public class JasperReportDemo
{

public static void main(String[] args) throws Exception
{
JasperReportDemo jrd = new JasperReportDemo();
jrd.reportMethod(jrd.getMap());
}


public void reportMethod(Map map) throws Exception
{
JasperReport jasperReport = null;
JasperPrint jasperPrint = null;


try
{

/**//*
* File file = new File("Person.jrxml"); InputStream in = new
* FileInputStream(file); // 編譯報表 jasperReport =
* JasperCompileManager.compileReport(in);
*/
// 實際中編譯報表很耗時,采用Ireport編譯好的報表
jasperReport = (JasperReport) JRLoader
.loadObject("D:\\workspace\\Person.jasper");
// 填充報表
jasperPrint = JasperFillManager
.fillReport(jasperReport, map, getDataSource());
// JasperExportManager.exportReportToHtmlFile(jasperPrint,
// "test.html");
JasperViewer jasperViewer = new JasperViewer(jasperPrint);
jasperViewer.setVisible(true);

} catch (JRException e)
{
e.printStackTrace();

} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}


public Map getMap()
{

Map map = new HashMap();
map.put("reportTitle", "laoshulin");
return map;
}


public JRDataSource getDataSource() throws Exception
{

// 自定義數據源
Person p1 = new Person();
p1.setAge("23");
p1.setDepartment("ISoftStone");
p1.setName("LAOSHULIN");
p1.setPassword("123456789");
p1.setPid("2008040516058772hj");
p1.setSex("man");
List<Person> list = new ArrayList<Person>();
list.add(p1);

JRDataSource datesource = new JRBeanCollectionDataSource(list);

return datesource;
}
}

看看效果吧!

簡簡單單的幾行代碼就可以完成一個簡單的報表,我們看到的這些工具欄圖表還有窗口的一些標題啊等等都可以自己的定制哦,下來慢慢介紹!
posted on 2008-12-12 09:56
凌晨風 閱讀(6335)
評論(10) 編輯 收藏 所屬分類:
iReport + JasperReport 系列