日期:2014-05-17  浏览次数:21003 次

JasperReport(4)——使用一个BeanCollectionDataSource

查看JasperReport的api可以知道,JasperReport给我们提供了丰富的数据源,其中BeanCollectionDataSource是一个比较常用的数据源。正如其名,BeanCollectionDataSource是把一个装满Bean对象的Collection作为数据源,在JasperReport的模版中定义的每一个Filed就相当于Bean对象的一个Filed。

?

下面是一个模版的定义,取名为report4.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report4" language="groovy" columnCount="2" pageWidth="800" pageHeight="842" columnWidth="380" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<parameter name="title" class="java.lang.String"/>
	<field name="id" class="java.lang.Integer"/>
	<field name="name" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="79" splitType="Stretch">
			<textField>
				<reportElement x="62" y="19" width="630" height="29"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="16"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
			</textField>
		</band>
	</title>
	<columnHeader>
		<band height="24" splitType="Stretch">
			<staticText>
				<reportElement x="226" y="2" width="100" height="20"/>
				<textElement textAlignment="Center">
					<font size="12" isBold="true"/>
				</textElement>
				<text><![CDATA[序号]]></text>
			</staticText>
			<staticText>
				<reportElement x="434" y="2" width="100" height="20"/>
				<textElement textAlignment="Center">
					<font size="12" isBold="true"/>
				</textElement>
				<text><![CDATA[名称]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="37" splitType="Stretch">
			<textField>
				<reportElement x="226" y="11" width="100" height="20"/>
				<textElement textAlignment="Center" verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="434" y="11" width="100" height="20"/>
				<textElement textAlignment="Center" verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
</jasperReport>

?

下面是进行测试的主程序:

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;

public class Test3 {

	public static void main(String args[]) throws JRException {
		
		//用于传递参数的Map
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("title", "JasperReport使用BeanCollectionDataSource示例");
		
		//用于建立BeanCollection数据源的Collection
		List<Product> beanCollection = new ArrayList<Product>();
		for (int i=0; i<10; ) {
			Product p = new Product();
			p.setId(++i);
			p.setName("商品_" + i);
			beanCollection.add(p);