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);
		}
		JRDataSource dataSource = new JRBeanCollectionDataSource(beanCollection);
		//编译jrxml文件
		JasperReport jasperReport = JasperCompileManager.compileReport("src/report4.jrxml");
		//填充数据
		JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
		//导出为html文件
		JasperExportManager.exportReportToHtmlFile(jasperPrint, "test3_beanCollectionDataSource.html");
		System.out.println(".........................");
	}
	
}

 

Product.class

public class Product {
	
	private int id; 
	
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值