故事:两个领域对象:
class JasperProject {
private String ...
}
class JasperProjectGroup {
private String ...
private List<JasperProject> ...
}
贾斯珀的代码:
Map<String, Object> parameters = new HashMap<String, Object>();
List<JasperProjectGroup> groups = buildGroups();
parameters.put("groups", groups);
InputStream jasperReportInputStream = ProjectStatusReportGenerator.class.getClassLoader().getResourceAsStream(
PROJECT_STATUS_JASPER_REPORT_FILENAME);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReportInputStream, parameters,
new JREmptyDataSource());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
return outputStream.toByteArray();
我想创建一个 pdf 报告,其中每个单独的页面上的每个组表(例如第 1 页上的第 1 组表,第 2 页上第 2 组的表)。但是,当我将组作为参数传递给 Jasper 模板时,它只显示一个表格,其中包含 pdf 报告中的所有组。
jrxml 的 XML 代码
第一个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="timesheets" pageWidth="595" pageHeight="842" 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"/>
<parameter name="groups" class="java.util.Collection"/>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="800" splitType="Stretch">
<subreport runToBottom="true">
<reportElement x="0" y="0" width="515" height="800"/>
<dataSourceExpression><![CDATA[(new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{groups}))]]></dataSourceExpression>
<subreportExpression><![CDATA[net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.class.getClassLoader().getResourceAsStream("projectgroup.jasper")]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
第二个模板(projectgroup.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="projectgroup" pageWidth="595" pageHeight="842" 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"/>
<subDataset name="TableDataset1">
<field name="projectName" class="java.lang.String"/>
<field name="bookedHours" class="java.lang.String"/>
<field name="predictedHours" class="java.lang.String"/>
<field name="burnedHours" class="java.lang.String"/>
<field name="percentageComplete" class="java.lang.String"/>
<field name="bookedHoursBurned" class="java.lang.String"/>
</subDataset>
<field name="jasperProjectStatusReports" class="java.util.List"/>
<detail>
<band height="50" splitType="Stretch">
<subreport>
<reportElement x="0" y="0" width="515" height="50"/>
<dataSourceExpression><![CDATA[(new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{jasperProjectStatusReports}))]]></dataSourceExpression>
<subreportExpression><![CDATA[net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.class.getClassLoader().getResourceAsStream("projectgroup_table.jasper")]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
第三个模板 xml (projectgroup_table.jasper):
<?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="projectgroup_table" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="groupName" class="java.lang.String"/>
<field name="projectName" class="java.lang.String"/>
<field name="bookedHours" class="java.lang.String"/>
<field name="predictedHours" class="java.lang.String"/>
<field name="burnedHours" class="java.lang.String"/>
<field name="percentageComplete" class="java.lang.String"/>
<field name="bookedHoursBurned" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="20" splitType="Stretch">
<textField>
<reportElement mode="Opaque" x="0" y="0" width="100" height="20" forecolor="#FFFFFF" backcolor="#006700"/>
<box leftPadding="5" rightPadding="5">
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{groupName}]]></textFieldExpression>
</textField>
<staticText>
<reportElement mode="Opaque" x="100" y="0" width="100" height="20" forecolor="#FFFFFF" backcolor="#006700"/>
<box leftPadding="5" rightPadding="5">
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Booked]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="200" y="0" width="100" height="20" forecolor="#FFFFFF" backcolor="#006700"/>
<box leftPadding="5" rightPadding="5">
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Predicted]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="300" y="0" width="100" height="20" forecolor="#FFFFFF" backcolor="#006700"/>
<box leftPadding="5" rightPadding="5">
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Burned]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="400" y="0" width="100" height="20" forecolor="#FFFFFF" backcolor="#006700"/>
<box leftPadding="5" rightPadding="5">
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Complete]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="500" y="0" width="95" height="20" forecolor="#FFFFFF" backcolor="#006700"/>
<box leftPadding="5" rightPadding="5">
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Booked Burned]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="21" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{projectName}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="0" rightPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{bookedHours}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="200" y="0" width="100" height="20"/>
<box leftPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{predictedHours}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="300" y="0" width="100" height="20"/>
<box leftPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{burnedHours}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="400" y="0" width="100" height="20"/>
<box leftPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{percentageComplete}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="500" y="1" width="95" height="20"/>
<box leftPadding="5">
<pen lineWidth="1.0"/>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{bookedHoursBurned}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
对此有什么想法吗?谢谢。