日期:2014-05-16  浏览次数:21204 次

JasperReport动态生成表格报表

在JasperReport中动态的生成报表有两种情况:

一、直接从数据库中查询把查询结果直接显示出来适合简单的报表;

二、手动传一个DataSuoce适合复杂报表。

完成效果图:



?
?

在页面显示字体比较复杂的时候往往是自己去传一个数据源,比如说要显示的字段是来自多张表的内容时,具体过程是这样的:

1.用iReport创建jrmxl文件

<?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="merclickreport" language="groovy" pageWidth="1020" pageHeight="842" columnWidth="980" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true" uuid="1f7516fd-fdf2-4832-bd65-e38f305e587c">
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	 <subDataset name="Table Dataset 1" uuid="beace6af-4d6c-4014-8611-e9409b9d6050">
		<queryString>
			<![CDATA[]]>
		</queryString>
		<field name="mber_nickname" class="java.lang.String"/>
		<field name="mber_username" class="java.lang.String"/>
		<field name="r_id" class="java.lang.Long"/>
		<field name="r_createtime" class="java.lang.String"/>
		<field name="ord_id" class="java.lang.Long"/>
	</subDataset>
	<queryString>
		<![CDATA[]]>
	</queryString>
	<field name="cgrlist" class="java.util.List">
		<fieldDescription><![CDATA[cgrlist]]></fieldDescription>
	</field>
	<detail>
	<band height="150">
		<componentElement>
			<reportElement uuid="07fec065-eeb0-4feb-a1bb-2f2350517e63" key="table" style="table" positionType="Float" stretchType="RelativeToTallestObject" x="13" y="33" width="955" height="80" isPrintWhenDetailOverflows="true"/>
			<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
				<datasetRun subDataset="Table Dataset 1" uuid="c5aa5d25-ef6c-44d9-8474-7ed2eefeee5a">
					<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cgrlist})]]></dataSourceExpression>
				</datasetRun>
				<jr:column width="102" uuid="d89caf07-d0fc-479d-9d7d-75d0c22eb5d6">
				<jr:columnHeader style="table_CH" height="60" rowSpan="1">
					<staticText>
						<reportElement uuid="ab907c87-9e75-4c48-b356-51c6720d806d" x="1" y="0" width="100" height="30"/>
						<textElement textAlignment="Center" verticalAlignment="Middle">							  <font isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
						</textElement>
						<text><![CDATA[会员登录名]]></text>
					</staticText>
					</jr:columnHeader>
					<jr:detailCell style="table_TD" height="30" rowSpan="1">
					<textField>
						<reportElement uuid="4526b26c-be8c-437f-add5-70e00e0ebece" x="1" y="0" width="100" height="30"/>
						  <textElement textAlignment="Center" verticalAlignment="Middle">
							<font pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
						  </textElement>
						<textFieldExpression><![CDATA[$F{mber_username}]]></textFieldExpression>
					</textField>
					</jr:detailCell>
				</jr:column>
				<jr:column width="74" uuid="862eeeaf-7934-4c69-86a8-4593ec7a2916">
					<jr:columnHeader style="table_CH" height="60" rowSpan="1">
						<staticText>
							<reportElement uuid="ab907c87-9e75-4c48-b356-51c6720d8