Java 在使用传递给报表的bean集合作为参数的情况下,表不显示

Java 在使用传递给报表的bean集合作为参数的情况下,表不显示,java,jasper-reports,Java,Jasper Reports,我有一个使用数据集的表。作为报告的标题显示为ok,但呈现的pdf文件中缺少该表。我传递给报表的集合包含十个元素 我使用的是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/XMLS

我有一个使用数据集的表。作为报告的标题显示为ok,但呈现的pdf文件中缺少该表。我传递给报表的集合包含十个元素

我使用的是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();
jrbeancollectiondatasourceitemsjrbean=新的JRBeanCollectionDataSource(listItems);
/*映射以保存Jasper报告参数*/
映射参数=新的HashMap();
参数put(“ItemDataSource”,itemsJRBean);
参数.put(“itemPath”,“c:/Temp/invoice_logo.png”);
/*使用jasper报表的编译版本(.jasper)生成PDF*/
JasperPrint JasperPrint=JasperFillManager.fillReport(localJasperTemplates+ExportResultsNumber.DEMO.getJasperReport(),参数,新的JReputyDataSource());
/*outputStream以创建PDF*/
OutputStream OutputStream=新文件OutputStream(新文件(“c:/Temp/jasper”+(新日期()).getTime())+”.pdf);
/*将内容写入PDF文件*/
JasperUnmanager.runReportToPdfStream(getTemplatePdf(ExportResultsEnum.DEMO)、outputStream、参数、新的JREptyDatasource());
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>

另一个需要改进的地方是,您可以使用whenNodeType属性设置表的whenSectionsNoDetail值,以便在集合为空时显示空表。使用您的代码,空输入集合将不存在该表

工作示例 报告模板 有效的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>

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);
}