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

JasperReport(1)——IReport的简单使用

JasperReport是一个用纯Java写的方便开发报表功能的开源软件。JasperReport的模版是类似于xml的格式,但是扩展名却是.jrxml。利用该模版就可以编译成.jasper文件,JasperReport就可以通过该.jasper文件生成相应的报表。

JasperReport的模版用手工来做的话是非常繁琐的,为此官方给我们提供了一个可视化工具叫IReport。

?

JasperReport生成报表的数据源可以是数据库、xml文件、excel文件等。而一般我们用IReport进行模版设计的时候用的比较多的还是利用数据库来设计。所以下面在使用IReport之前先给IReport设置一个数据源。




?

选择下一步

?


?

设置了数据源之后,我们就来建立一个简单的报表,但是该报表不会包含任何的数据源,只会有些简单的组件

选择文件->new ,打开新建页面,选择报表,如下图所示

?之后就一直下一步,就会打开一个刚刚建立的报表,模样如下图所示:

?


JasperReport是包含以下部分的:Title、Page Header、Column Header、 Detail、Column Footer、Page Footer和Summary,这几部分并不是每个部分都必须要的。

?

下面是一个通过左边的组件面板里面的static text组件生成的一个简单报表样式


生成的xml文件:

<?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="report3" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" 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"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="44" splitType="Stretch">
			<staticText>
				<reportElement x="227" y="12" width="100" height="20" forecolor="#FF0000"/>
				<textElement textAlignment="Center">
					<font size="13" isBold="true"/>
				</textElement>
				<text><![CDATA[标题]]></text>
			</staticText>
		</band>
	</title>
	<pageHeader>
		<band height="35" splitType="Stretch">
			<staticText>
				<reportElement x="227" y="15" width="100" height="20" forecolor="#FF0000"/>
				<textElement textAlignment="Center">
					<font size="13" isBold="true"/>
				</textElement>
				<text><![CDATA[页眉]]></text>
			</staticText>
		</band>
	</pageHeader>
	<columnHeader>
		<band height="40" splitType="Stretch">
			<staticText>
				<reportElement x="227" y="12" width="100" height="20" forecolor="#FF0000"/>
				<textElement textAlignment="Center">
					<font size="13" isBold="true"/>
				</textElement>
				<text><![CDATA[相当于表头]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="44" splitType="Stretch">
			<staticText>
				<reportElement x="227" y="13" width="100" height="20" forecolor="#FF0000"/>
				<textElement textAlignment="Center">
					<font size="13" isBold="true"/>
				</textElement>
				<text><![CDATA[报表内容]]></text>
			</staticText>
		</band>
	</detail>
	<columnFooter>
		<band height="40" splitType="Stretch">
			<staticText>
				<reportElement x="227" y="10" width="100" height="20" forecolor="#FF0000"/>
				<textElement textAlignment="Center">
					<font size="13" isBold="true"/>
				</textElement>
				<text><![CDATA[相当于表尾]]></text>