【问题标题】:How can I get data into a Table in JasperReports? [duplicate]如何将数据放入 JasperReports 中的表格中? [复制]
【发布时间】:2018-07-16 19:27:04
【问题描述】:

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

this example 之后,我已切换到在参数中发送数据。这在报告的第 2 页上提供了一个空表(示例本身有效)。

我的代码如下。

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

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

Java

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>

【问题讨论】:

  • 每次编译.jrxml 文件通常没有任何好处。改为编译、加载和填充 .jasper 文件。 (这可能不会解决问题,但会提高性能。)另外,考虑使用继承样式来消除线宽和边框周围的一些重复。
  • 我将其保留为 jrxml,因为我对 jrxml 进行了一些手动编辑以使用参数而不是数据源,并进行了一些其他更改以使其工作。如果我无法将数据放入表中,那么优化这个是没有意义的。

标签: java jasper-reports


【解决方案1】:

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

替代解决方案是:

1) 对象列表作为主集合

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

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

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

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

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

3) 对象列表作为参数

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

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

关于jrxml中的错误

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-14
    • 2018-04-04
    • 2013-05-27
    • 2017-08-29
    • 2021-12-29
    • 1970-01-01
    • 2012-10-12
    • 2020-05-10
    相关资源
    最近更新 更多