Am not experienced much in JasperReports but this is my second assignment and am stuck with this issue
Below is my Java bean classes Report and 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()....
}
And i am exporting report like below,
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();
And below is the Jasper code, i removed other label related code and added only necessary code here,
<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>
And the report supposed tobe generated like below,
But it is generating as below, its merging all List to one Report object,
Please ask if any other details needed regarding this, help me.
The logic of Jasper Reports is to first generate the pageHeader
, columnHeader
(called once on each page) and then to iterate the detail
band on your datasource.
This is why you are having the current result. The pageHeader
, columnHeader
is create when you are at record one, then the list in detail band is called twice on your datasource.
You should to move Report, Field1,.. Field4
into the detail band, so also these are displayed as datasource is iterated.