JasperReports:列表对象在生成报告时与其他对象列表合并

时间:2016-05-24 04:05:04

标签: java jasper-reports

在JasperReports中没有多少经验,但这是我的第二个任务,我坚持这个问题

以下是我的Java bean类Report和ReportRecords,

Class Report{
    private String field1;
    private String field2;
    private String field3;
    private String field4;
    List<ReportRecords> reportRecordData = new ArrayList<>();

    ....setters getters()....
}

Class ReportRecords{
    private String column1;
    private String column2;
    private String column3;
    private String column4;

    ....setters getters()....
}

我正在导出如下报告,

    List<Report> report = getReports();

    JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(report);
    Map<String, Object> parameters = new HashMap<String, Object>();
    JasperPrint printFileName = JasperFillManager.fillReport(template, parameters, beanColDataSource);
    JRTextExporter txtExporter = new JRTextExporter();

    txtExporter.setParameter(JRTextExporterParameter.LINE_SEPARATOR, "\r\n");
    txtExporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, 110);
    txtExporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, 80);
    txtExporter.setParameter(JRExporterParameter.JASPER_PRINT, printFileName);
    txtExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "C:/report/report.txt");
    txtExporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH,new Float(7));
    txtExporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT,new Float(13));
    txtExporter.exportReport();

以下是Jasper代码,我删除了其他与标签相关的代码,并在此处仅添加了必要的代码,

<subDataset name="serviceCodeData" uuid="6b6aa9cd-83d1-47b6-b475-2988e1a3a804">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="column1" class="java.lang.String"/>
    <field name="column2" class="java.lang.String"/>
    <field name="column3" class="java.lang.String"/>
    <field name="column4" class="java.lang.String"/>
</subDataset>
<field name="Field1" class="java.lang.String"/>
<field name="Field2" class="java.lang.String"/>
<field name="Field3" class="java.lang.String"/>
<field name="Field4" class="java.lang.String"/>

<pageHeader>
    <!-- All Field1 to Field4 labels and values -->
</pageHeader>
<columnHeader>
    <!-- All column1 to column2 labels -->
</columnHeader>

    And below is the list for column values

<detail>
    <band height="19" splitType="Stretch">
        <componentElement>
            <reportElement x="-19" y="0" width="818" height="19" uuid="d08327de-369c-41b5-818a-2381d0f7def2"/>
            <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                <datasetRun subDataset="reportRecordData" uuid="f39ce5d6-7347-4911-b211-3c60cbd01590">
                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{reportRecordData})]]></dataSourceExpression>
                </datasetRun>
                <jr:listContents height="19" width="818">
                    <textField>
                        <reportElement x="39" y="0" width="157" height="18" uuid="03ef5ad6-fafb-4c5d-aa41-a46b1474f9fd"/>
                        <textElement textAlignment="Left"/>
                        <textFieldExpression><![CDATA[$F{column1}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="39" y="0" width="157" height="18" uuid="03ef5ad6-fafb-4c5d-aa41-a46b1474f9fd"/>
                        <textElement textAlignment="Left"/>
                        <textFieldExpression><![CDATA[$F{column2}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="39" y="0" width="157" height="18" uuid="03ef5ad6-fafb-4c5d-aa41-a46b1474f9fd"/>
                        <textElement textAlignment="Left"/>
                        <textFieldExpression><![CDATA[$F{column3}}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="39" y="0" width="157" height="18" uuid="03ef5ad6-fafb-4c5d-aa41-a46b1474f9fd"/>
                        <textElement textAlignment="Left"/>
                        <textFieldExpression><![CDATA[$F{column4}]]></textFieldExpression>
                    </textField>
                </jr:listContents>
            </jr:list>
        </componentElement>
    </band>
</detail>

报告应该生成如下,

enter image description here

但它生成如下,它将所有List合并到一个Report对象,

enter image description here

请询问是否需要有任何其他详细信息,请帮助我。

1 个答案:

答案 0 :(得分:1)

Jasper Reports的逻辑是首先生成pageHeadercolumnHeader(每页调用一次),然后迭代数据源上的detail频段。

这就是您获得当前结果的原因。当您处于记录1时,将创建pageHeadercolumnHeader,然后在数据源上调用详细信息带中的列表两次。

您应该将Report, Field1,.. Field4 移动到详细信息区,因此这些也会显示为迭代数据源。