jasperreport学习 之 javabean封装成list作为数据源

jasperreport学习之javabean封装成list作为数据源

              原创 by javaliujie  http://javaliujie.iteye.com/blog/278936

 

1.思想

 javabean
    ↓
    ↓封装
    ↓
Collection
    ↓
    ↓作为数据源
    ↓
JRDataSource

 

2.代码部分

CustomBean.java

 

 

package cn.com.jr;

public class CustomBean
{
	private String city = null;
	private Integer id = null;
	private String name = null;
	private String street = null;

	public CustomBean(String pcity,Integer pid,String pname,String pstreet)
	{
		city = pcity;
		id = pid;
		name = pname;
		street = pstreet;
	}
	public CustomBean getMe()
	{
		return this;
	}
	public String getCity()
	{
		return city;
	}
	public Integer getId()
	{
		return id;
	}
	public String getName()
	{
		return name;
	}
	public String getStreet()
	{
		return street;
	}
}

  

 

 

 

 

CustomBeanFactory.java

 

package cn.com.jr;

import java.util.Arrays;
import java.util.Collection;

public class CustomBeanFactory
{
	private static CustomBean[] data =
		{
			new CustomBean("Berne", new Integer(9), "James Schneider", "277 Seventh Av."),
			new CustomBean("Berne", new Integer(22), "Bill Ott", "250 - 20th Ave."),
			new CustomBean("Boston", new Integer(23), "Julia Heiniger", "358 College Av."),
			new CustomBean("Boston", new Integer(32), "Michael Ott", "339 College Av."),
			new CustomBean("Chicago", new Integer(39), "Mary Karsen", "202 College Av."),
			new CustomBean("Chicago", new Integer(35), "George Karsen", "412 College Av."),
			new CustomBean("Chicago", new Integer(11), "Julia White", "412 Upland Pl."),
			new CustomBean("Dallas", new Integer(47), "Janet Fuller", "445 Upland Pl."),
			new CustomBean("Dallas", new Integer(43), "Susanne Smith", "2 Upland Pl."),
			new CustomBean("Dallas", new Integer(40), "Susanne Miller", "440 - 20th Ave."),
			new CustomBean("Dallas", new Integer(36), "John Steel", "276 Upland Pl."),
			new CustomBean("Dallas", new Integer(37), "Michael Clancy", "19 Seventh Av."),
			new CustomBean("Dallas", new Integer(19), "Susanne Heiniger", "86 - 20th Ave."),
			new CustomBean("Dallas", new Integer(10), "Anne Fuller", "135 Upland Pl."),
			new CustomBean("Dallas", new Integer(4), "Sylvia Ringer", "365 College Av."),
			new CustomBean("Dallas", new Integer(0), "Laura Steel", "429 Seventh Av."),
			new CustomBean("Lyon", new Integer(38), "Andrew Heiniger", "347 College Av."),
			new CustomBean("Lyon", new Integer(28), "Susanne White", "74 - 20th Ave."),
			new CustomBean("Lyon", new Integer(17), "Laura Ott", "443 Seventh Av."),
			new CustomBean("Lyon", new Integer(2), "Anne Miller", "20 Upland Pl."),
			new CustomBean("New York", new Integer(46), "Andrew May", "172 Seventh Av."),
			new CustomBean("New York", new Integer(44), "Sylvia Ott", "361 College Av."),
			new CustomBean("New York", new Integer(41), "Bill King", "546 College Av."),
			new CustomBean("Oslo", new Integer(45), "Janet May", "396 Seventh Av."),
			new CustomBean("Oslo", new Integer(42), "Robert Ott", "503 Seventh Av."),
			new CustomBean("Paris", new Integer(25), "Sylvia Steel", "269 College Av."),
			new CustomBean("Paris", new Integer(18), "Sylvia Fuller", "158 - 20th Ave."),
			new CustomBean("Paris", new Integer(5), "Laura Miller", "294 Seventh Av."),
			new CustomBean("San Francisco", new Integer(48), "Robert White", "549 Seventh Av."),
			new CustomBean("San Francisco", new Integer(7), "James Peterson", "231 Upland Pl.")
		};  
	public static Object[] getBeanArray()
	{
		return data;
	}
	public static Collection getBeanCollection()
	{
		return Arrays.asList(data);
	}


}

 

 

 

 

 

 

 

 

3.iReport设置

  a.设置环境变量

 

 

 

 

 

 

 

 

  b.查询获得变量

 

 

c.iReport页面

 

 

 

  d.iReport最终效果图

 

 

 

 

4.与程序结合代码

 

CustomDataSource.java

package cn.com.jr;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

public class CustomDataSource implements JRDataSource
{
	private Object[][] data =
		{
			{"Berne", new Integer(22), "Bill Ott", "250 - 20th Ave."},
			{"Berne", new Integer(9), "James Schneider", "277 Seventh Av."},
			{"Boston", new Integer(32), "Michael Ott", "339 College Av."},
			{"Boston", new Integer(23), "Julia Heiniger", "358 College Av."},
			{"Chicago", new Integer(39), "Mary Karsen", "202 College Av."},
			{"Chicago", new Integer(35), "George Karsen", "412 College Av."},
			{"Chicago", new Integer(11), "Julia White", "412 Upland Pl."},
			{"Dallas", new Integer(47), "Janet Fuller", "445 Upland Pl."},
			{"Dallas", new Integer(43), "Susanne Smith", "2 Upland Pl."},
			{"Dallas", new Integer(40), "Susanne Miller", "440 - 20th Ave."},
			{"Dallas", new Integer(36), "John Steel", "276 Upland Pl."},
			{"Dallas", new Integer(37), "Michael Clancy", "19 Seventh Av."},
			{"Dallas", new Integer(19), "Susanne Heiniger", "86 - 20th Ave."},
			{"Dallas", new Integer(10), "Anne Fuller", "135 Upland Pl."},
			{"Dallas", new Integer(4), "Sylvia Ringer", "365 College Av."},
			{"Dallas", new Integer(0), "Laura Steel", "429 Seventh Av."},
			{"Lyon", new Integer(38), "Andrew Heiniger", "347 College Av."},
			{"Lyon", new Integer(28), "Susanne White", "74 - 20th Ave."},
			{"Lyon", new Integer(17), "Laura Ott", "443 Seventh Av."},
			{"Lyon", new Integer(2), "Anne Miller", "20 Upland Pl."},
			{"New York", new Integer(46), "Andrew May", "172 Seventh Av."},
			{"New York", new Integer(44), "Sylvia Ott", "361 College Av."},
			{"New York", new Integer(41), "Bill King", "546 College Av."},
			{"Oslo", new Integer(45), "Janet May", "396 Seventh Av."},
			{"Oslo", new Integer(42), "Robert Ott", "503 Seventh Av."},
			{"Paris", new Integer(25), "Sylvia Steel", "269 College Av."},
			{"Paris", new Integer(18), "Sylvia Fuller", "158 - 20th Ave."},
			{"Paris", new Integer(5), "Laura Miller", "294 Seventh Av."},
			{"San Francisco", new Integer(48), "Robert White", "549 Seventh Av."},
			{"San Francisco", new Integer(7), "James Peterson", "231 Upland Pl."}
		};

	private int index = -1;
	public CustomDataSource()
	{
	}
	public boolean next() throws JRException
	{
		index++;
		return (index < data.length);
	}

	public Object getFieldValue(JRField field) throws JRException
	{
		Object value = null;
		
		String fieldName = field.getName();
		
		if ("the_city".equals(fieldName))
		{
			value = data[index][0];
		}
		else if ("id".equals(fieldName))
		{
			value = data[index][1];
		}
		else if ("name".equals(fieldName))
		{
			value = data[index][2];
		}
		else if ("street".equals(fieldName))
		{
			value = data[index][3];
		}
		return value;
	}
}

 

 

 

TestPDF.java

public class TestPdf { 

    public static void main(String args[]) {
          File reportFile = new File("javabeantest.jasper");
          CustomDataSource cds = new CustomDataSource() ;
          try {
			JasperRunManager.runReportToPdfFile(reportFile.getPath(), "c:\\javabeantest.pdf", new HashMap(), cds) ;
		} catch (JRException e) {
			e.printStackTrace();
		}
    } 
}

原创 by javaliujie  http://javaliujie.iteye.com/blog/278936

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值