首页 > 解决方案 > 如何将数据放入 JasperReports 中的表格中?

问题描述

我无法将数据放入表中。我使用的是第 14 章中的示例,只是我使用 Collection 而不是报告数据源。JasperFillManager.fillReport()在 a 中使用 Collection调用JRBeanCollectionDataSource不会显示任何数据,但会为 Collection 的每个元素创建一个空白页面。

按照这个例子,我已经切换到在参数中发送数据。这在报告的第 2 页上给出了一个空表(示例本身有效)。

我的代码如下。

IntelliJ 抱怨 JasperSoft Studio 生成的 jrxml 无效。它说元素不能在它所在的位置。也许这与它有关。

如何将数据放入 JasperReports 中的表格中?

爪哇

public class JasperReportsLineItemTableExample {

    public static Logger logger = LoggerFactory.getLogger(JasperReportsLineItemTableExample.class);

    public static void main(String[] args) {

        String reportJrxmlFile = "LineItems_Table_A4.jrxml";
        String outputFile = "LineItemsTableExample.pdf";

        List<LineItem> items = LineItemsUtil.createItems(10);
        logger.info(items.size() + " items");
        JRBeanCollectionDataSource itemsJRBean = new JRBeanCollectionDataSource(items);

        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("LineItemDataSource", itemsJRBean);


        try {
            JasperReport jasperReport = JasperCompileManager.compileReport(reportJrxmlFile);
//            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, itemsJRBean);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());

            try (OutputStream outputStream = new FileOutputStream(new File(outputFile))) {
                JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);

                System.out.println("File Generated");

            } catch (IOException | JRException ex) {
                ex.printStackTrace();
            }
        } catch (JRException ex) {
            ex.printStackTrace();
        }
    }
}

JRXML

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="LineItems_Table_A4" pageWidth="595" pageHeight="842" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="565ea393-c6d0-4128-8564-05701d3de374">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Dataset1" uuid="57478514-4d8c-49d7-a6e5-8566d1ec7332">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="itemNumber" class="java.lang.String">
            <fieldDescription><![CDATA[itemNumber]]></fieldDescription>
        </field>
        <field name="quantity" class="java.lang.Integer">
            <fieldDescription><![CDATA[quantity]]></fieldDescription>
        </field>
        <field name="unitCost" class="java.lang.Float">
            <fieldDescription><![CDATA[unitCost]]></fieldDescription>
        </field>
        <field name="totalCost" class="java.lang.Float">
            <fieldDescription><![CDATA[totalCost]]></fieldDescription>
        </field>
    </subDataset>
    <parameter name="LineItemDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="260" y="34" width="100" height="30" uuid="096a5061-5710-4597-9ce7-e4a7edbfe155"/>
                <text><![CDATA[LineItems Table]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="357" splitType="Stretch">
            <componentElement>
                <reportElement positionType="FixRelativeToBottom" x="60" y="110" width="440" height="200" uuid="657ac0a8-31c5-4336-8e8d-fdec5dc92bd6">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
                    <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
                    <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
                </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="Dataset1" uuid="ac1740d4-8a9f-409d-979c-ad8788815312">
                        <dataSourceExpression><![CDATA[$P{LineItemDataSource} ]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="100" uuid="a4de9f35-c7a8-4d2d-8ea1-339144d7d3d4">
                        <jr:tableHeader style="Table_TH" height="30"/>
                        <jr:tableFooter style="Table_TH" height="30"/>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="7ea912f2-e802-4b7a-9ccb-3e7b3762f862"/>
                                <text><![CDATA[itemNumber]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="6e4662e4-9421-4328-8d00-766416414c6d"/>
                                <textFieldExpression><![CDATA[$F{itemNumber}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100" uuid="d10a0d67-02ea-4153-a799-6b4dbdd69200">
                        <jr:tableHeader style="Table_TH" height="30"/>
                        <jr:tableFooter style="Table_TH" height="30"/>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="4ba9a6bc-e6ed-477e-ae2f-0d91b27cd42f"/>
                                <text><![CDATA[quantity]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="366e5229-7cb8-441c-9a2a-82535212f06c"/>
                                <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100" uuid="266fdaed-4f71-43d5-8515-89604973dcd9">
                        <jr:tableHeader style="Table_TH" height="30"/>
                        <jr:tableFooter style="Table_TH" height="30"/>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="50" height="30" uuid="c8bbc714-10b6-4305-bf55-9feb87c32498"/>
                                <text><![CDATA[unitCost]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="9db777d5-428c-4387-8e9b-02aa4422407e"/>
                                <textFieldExpression><![CDATA[$F{unitCost}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100" uuid="b70c951b-951d-443f-9c21-8acf8b025f64">
                        <jr:tableHeader style="Table_TH" height="30"/>
                        <jr:tableFooter style="Table_TH" height="30"/>
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="b9b914be-ab50-4e2e-bf52-fca2a891a7de"/>
                                <text><![CDATA[total Cost]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table_CH" height="30"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="071819a9-ab97-41df-b2b7-b8e9783dfb4d"/>
                                <textFieldExpression><![CDATA[$F{totalCost}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

标签: javajasper-reports

解决方案


您将对象作为主集合传递。您必须将该字段添加为根字段 而不是子数据集字段(请参阅下面的解决方案 1)

替代解决方案是:

1) 对象列表作为主集合

这是您选择的第一个解决方案。

使用数据作为主要数据集合,并将其字段设置为根字段,并直接在您的详细信息带中使用

不需要表格,使用 columnn header band 将表格标题和详细信息带显示为表格内容。将为主数据源中的每个对象显示一个详细信息带。仅当整个报表只有一个表时才使用此选项(否则请考虑使用子报表)

2) 对象列表作为主集合第一项的字段

将此字段设置为根字段并将其作为表的数据源使用new JRBeanCollection(myField)

3) 对象列表作为参数

这是您选择的第二个解决方案。

这里有一个警告:这仅在您在标题带中使用数据时才有效,因为如果主集合为空或为空,则不会显示详细带。

关于jrxml中的错误

我猜这来自<![CDATA[]]>. Xml 解析器将其视为未封闭的标记,而这是由 jasper 解释的。我在正确的代码上也有这些错误。

如果您需要更多详细信息,请发表评论。


推荐阅读