Table 在使用 Collection 的 bean 作为参数传递给报告的情况下不显示

Table is not showing in case using Collection of beans passed to the report as parameter

我有一个使用数据集的 table。 header 作为报告显示正常,但 table 在呈现的 pdf 文件中不存在。我传递给报告的 collection 包含十个元素。

我正在使用 JasperReports 版本 6.6.0

这是我的模板:

<?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="Example" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e5e9a134-e531-4343-bd54-4957d1313922">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <subDataset name="ItemDataset1" uuid="1a019c16-d780-42e0-87e4-ff47b50120bf">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <parameter name="id" class="java.lang.Integer"/>
        <parameter name="xkey" class="java.lang.String"/>
        <parameter name="phone" class="java.lang.String"/>
        <parameter name="value" class="java.lang.String"/>
        <queryString>
            <![CDATA[]]>
        </queryString>
    </subDataset>
    <parameter name="itemPath" class="java.lang.String"/>
    <parameter name="ItemDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="87" splitType="Stretch">
            <staticText>
                <reportElement x="280" y="50" width="100" height="30" uuid="3c6b9a64-d8a6-4f64-9b1d-30c1afadcffc"/>
                <text><![CDATA[Static Text]]></text>
            </staticText>
            <line>
                <reportElement x="0" y="80" width="556" height="1" uuid="0c2ac013-5838-4910-b968-9201b61177c4"/>
            </line>
            <staticText>
                <reportElement x="415" y="10" width="50" height="20" uuid="f401dc0d-eeeb-4a2f-af5e-466c3d778f17"/>
                <text><![CDATA[Date:]]></text>
            </staticText>
            <textField pattern="EEEEE dd MMMMM yyyy">
                <reportElement x="465" y="10" width="84" height="20" uuid="0384c581-66f3-4060-87c1-cf02bbe26b2f"/>
                <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
            </textField>
            <textField pattern="EEEEE dd MMMMM yyyy">
                <reportElement x="465" y="30" width="84" height="20" uuid="9bbe3973-0e42-477c-b534-9f2bec0fb159"/>
                <textFieldExpression><![CDATA["INV0001"]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="415" y="30" width="50" height="20" uuid="9e79aa2d-56f5-45c6-a6ec-066ce401f681"/>
                <text><![CDATA[Invoice #]]></text>
            </staticText>
            <staticText>
                <reportElement x="95" y="0" width="210" height="25" uuid="894fb282-bc78-4873-bda3-5674978d0e79"/>
                <text><![CDATA[Your company name]]></text>
            </staticText>
            <staticText>
                <reportElement x="95" y="55" width="205" height="15" uuid="2d7bdc02-eee8-42cb-aa1d-3357f88dafc8"/>
                <text><![CDATA[Phone number, web address, ecc.]]></text>
            </staticText>
            <staticText>
                <reportElement x="95" y="25" width="205" height="15" uuid="9886959a-ae96-4260-97e7-7e09eafaeb36"/>
                <text><![CDATA[Street address]]></text>
            </staticText>
            <staticText>
                <reportElement x="95" y="40" width="205" height="15" uuid="b6b06786-3300-4126-9b29-3bd157ec8e46"/>
                <text><![CDATA[City, street, ZIP code]]></text>
            </staticText>
            <image>
                <reportElement x="10" y="10" width="50" height="50" uuid="c3c60645-c230-4fba-8718-54e1d36deee8"/>
                <imageExpression><![CDATA[$P{itemPath}]]></imageExpression>
            </image>
        </band>
    </title>
    <detail>
        <band height="226" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="0" width="555" height="226" uuid="a79e5d64-ff66-412c-a763-776f8ab50bca">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                </reportElement>
                <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="ItemDataset1" uuid="5743b1bc-2310-4b27-83fe-a82a61946761">
                        <dataSourceExpression><![CDATA[$P{ItemDataSource}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:columnGroup width="555" uuid="a88881d5-12e6-4dae-ad4c-efa14be9e224">
                        <jr:tableHeader height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="555" height="30" uuid="e8f2dfda-5c9a-403e-a6a3-1fa7ab435c4e"/>
                                <text><![CDATA[Resultados]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:column width="120" uuid="5c5eb73a-18fa-41a6-9365-cbf76ebf5a85">
                            <jr:tableHeader height="30" rowSpan="1"/>
                            <jr:columnHeader height="30" rowSpan="1">
                                <staticText>
                                    <reportElement x="0" y="0" width="120" height="30" uuid="75d40c9b-a962-419b-b97f-ef3a6a4b8d19"/>
                                    <text><![CDATA[Id]]></text>
                                </staticText>
                            </jr:columnHeader>
                            <jr:detailCell height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="120" height="30" uuid="78ed17bd-45ab-4acb-961d-024b9997e8b5"/>
                                    <textFieldExpression><![CDATA[$P{id}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column width="120" uuid="b56c5203-a671-4c97-bcd9-4cb46b0f6384">
                            <jr:tableHeader height="30" rowSpan="1"/>
                            <jr:columnHeader height="30" rowSpan="1">
                                <staticText>
                                    <reportElement x="0" y="0" width="120" height="30" uuid="3585cb03-3cc7-4e2d-b41a-86f396b8e665"/>
                                    <text><![CDATA[xkey]]></text>
                                </staticText>
                            </jr:columnHeader>
                            <jr:detailCell height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="120" height="30" uuid="7bad9819-f9d1-4a54-8198-698863d3623b"/>
                                    <textFieldExpression><![CDATA[$P{xkey}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column width="120" uuid="664cae4a-d7ae-4bea-86c8-03017349066a">
                            <jr:tableHeader height="30" rowSpan="1"/>
                            <jr:columnHeader height="30" rowSpan="1">
                                <staticText>
                                    <reportElement x="0" y="0" width="120" height="30" uuid="a960c5be-9011-49bf-9ee1-750d33194b58"/>
                                    <text><![CDATA[Phone]]></text>
                                </staticText>
                            </jr:columnHeader>
                            <jr:detailCell height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="120" height="30" uuid="9b0f1b26-21ce-48c4-b6ed-e9bc0148552d"/>
                                    <textFieldExpression><![CDATA[$P{phone}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column width="195" uuid="cb1ca676-5ca5-4882-b98d-51b3027fde2b">
                            <jr:tableHeader height="30" rowSpan="1"/>
                            <jr:columnHeader height="30" rowSpan="1">
                                <staticText>
                                    <reportElement x="0" y="0" width="195" height="30" uuid="a4a8d3fa-7e02-483c-9639-e3a94b54a11f"/>
                                    <text><![CDATA[Value]]></text>
                                </staticText>
                            </jr:columnHeader>
                            <jr:detailCell height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="195" height="30" uuid="62641d38-65b1-4742-8773-05b89342c30c"/>
                                    <textFieldExpression><![CDATA[$P{value}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                    </jr:columnGroup>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

这是我生成报告的代码:

   JRBeanCollectionDataSource itemsJRBean = new JRBeanCollectionDataSource(listItems);
    /* Map to hold Jasper report Parameters */
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("ItemDataSource", itemsJRBean);
    parameters.put("itemPath","c:/Temp/invoice_logo.png");

    /* Using compiled version(.jasper) of Jasper report to generate PDF */
    JasperPrint jasperPrint = JasperFillManager.fillReport(localJasperTemplates+ExportResultsEnum.DEMO.getJasperReport(), parameters, new JREmptyDataSource());
    /* outputStream to create PDF */
    OutputStream outputStream = new FileOutputStream(new File("c:/Temp/jasper"+(new Date()).getTime())+".pdf");
    /* Write content to PDF file */
    JasperRunManager.runReportToPdfStream(getTemplatePdf(ExportResultsEnum.DEMO), outputStream, parameters, new JREmptyDataSource());
    outputStream.close();

试了很多方法都解决不了

问题的原因

您使用的数据源有误 - 字段 应该在 ItemDataset1 声明,而不是参数

如果使用带字段的数据源,报告将正常。你的情况下的有效声明应该是这样的:

<subDataset name="ItemDataset1">
    <field name="id" class="java.lang.Integer"/>
    <field name="xkey" class="java.lang.String"/>
    <field name="phone" class="java.lang.String"/>
    <field name="value" class="java.lang.String"/>
</subDataset>

另一件需要改进的事情 - 您可以设置 whenNoDataType 属性和 AllSectionsNoDetail 值table 显示空 table 以防集合为空。使用您的代码,空输入集合将不存在 table。

工作示例

报告模板

有效的 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="Example" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" >
    <subDataset name="ItemDataset1" >
        <field name="id" class="java.lang.Integer"/>
        <field name="xkey" class="java.lang.String"/>
        <field name="phone" class="java.lang.String"/>
        <field name="value" class="java.lang.String"/>
    </subDataset>
    <parameter name="itemPath" class="java.lang.String"/>
    <parameter name="ItemDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <title>
        <band height="87" splitType="Stretch">
            <line>
                <reportElement x="0" y="80" width="556" height="1" />
            </line>
            <textField>
                <reportElement x="465" y="30" width="84" height="20" />
                <textFieldExpression><![CDATA["INV0001"]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="415" y="30" width="50" height="20" />
                <text><![CDATA[Invoice #]]></text>
            </staticText>
            <staticText>
                <reportElement x="95" y="0" width="210" height="25" />
                <text><![CDATA[Your company name]]></text>
            </staticText>
            <staticText>
                <reportElement x="95" y="55" width="205" height="15" />
                <text><![CDATA[Phone number, web address, ecc.]]></text>
            </staticText>
            <staticText>
                <reportElement x="95" y="25" width="205" height="15" />
                <text><![CDATA[Street address]]></text>
            </staticText>
            <staticText>
                <reportElement x="95" y="40" width="205" height="15" />
                <text><![CDATA[City, street, ZIP code]]></text>
            </staticText>
            <image>
                <reportElement x="10" y="10" width="50" height="50" />
                <imageExpression><![CDATA[$P{itemPath}]]></imageExpression>
            </image>
        </band>
    </title>
    <detail>
        <band height="226" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="0" width="555" height="226" >
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                </reportElement>
                <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" whenNoDataType="AllSectionsNoDetail">
                    <datasetRun subDataset="ItemDataset1" >
                        <dataSourceExpression><![CDATA[$P{ItemDataSource}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:columnGroup width="555" >
                        <jr:tableHeader height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="555" height="30" />
                                <text><![CDATA[Resultados]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:column width="120" >
                            <jr:tableHeader height="30" rowSpan="1"/>
                            <jr:columnHeader height="30" rowSpan="1">
                                <staticText>
                                    <reportElement x="0" y="0" width="120" height="30" />
                                    <text><![CDATA[Id]]></text>
                                </staticText>
                            </jr:columnHeader>
                            <jr:detailCell height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="120" height="30" />
                                    <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column width="120" >
                            <jr:tableHeader height="30" rowSpan="1"/>
                            <jr:columnHeader height="30" rowSpan="1">
                                <staticText>
                                    <reportElement x="0" y="0" width="120" height="30" />
                                    <text><![CDATA[xkey]]></text>
                                </staticText>
                            </jr:columnHeader>
                            <jr:detailCell height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="120" height="30" />
                                    <textFieldExpression><![CDATA[$F{xkey}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column width="120" >
                            <jr:tableHeader height="30" rowSpan="1"/>
                            <jr:columnHeader height="30" rowSpan="1">
                                <staticText>
                                    <reportElement x="0" y="0" width="120" height="30" />
                                    <text><![CDATA[Phone]]></text>
                                </staticText>
                            </jr:columnHeader>
                            <jr:detailCell height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="120" height="30" />
                                    <textFieldExpression><![CDATA[$F{phone}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column width="195" >
                            <jr:tableHeader height="30" rowSpan="1"/>
                            <jr:columnHeader height="30" rowSpan="1">
                                <staticText>
                                    <reportElement x="0" y="0" width="195" height="30" />
                                    <text><![CDATA[Value]]></text>
                                </staticText>
                            </jr:columnHeader>
                            <jr:detailCell height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="195" height="30" />
                                    <textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                    </jr:columnGroup>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

我在 subDataset 中使用了在 textFields 表达式中声明的字段。

构建报告的Java代码

pdf 格式的构建报告片段:

JasperReport jasperReport;

try (InputStream inputStream = getClass().getResourceAsStream(pathToTemplate)) {
    jasperReport = JasperCompileManager.compileReport(JRXmlLoader.load(inputStream));
}

Map<String, Object> params = new HashMap<>();

Collection<BeanForTable> collection = Arrays.asList(
        BeanForTable.builder().id(1).phone("101").value("value1").xkey("key1").build(),
        BeanForTable.builder().id(2).phone("707").value("value2").xkey("key2").build()
);

params.put("ItemDataSource", new JRBeanCollectionDataSource(collection));
params.put("itemPath", pathToImage);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());

SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
File outputFile = new File(outputFileName);
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
     OutputStream fileOutputStream = new FileOutputStream(outputFile)) {
    JRPdfExporter exporter = new JRPdfExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(byteArrayOutputStream));
    exporter.setConfiguration(configuration);
    exporter.exportReport();
    byteArrayOutputStream.writeTo(fileOutputStream);
}

在此代码段中,报告生成为 pdf 文件。

生成的输出

注释

请不要忘记,如果在 JasperFillManager.fillReport 方法中通过第三个参数传递 new JREmptyDataSource(0),报告将是空的(甚至 Title band 将不存在)因为:

  1. 您将 jr:table 置于 详细信息
  2. 您没有将 whenNoDataType 报告的属性设置为 AllSectionsNoDetail