【问题标题】:use Table dataset field value in main report在主报告中使用表数据集字段值
【发布时间】:2017-04-22 04:56:39
【问题描述】:
<?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="TestingImage" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true" uuid="5470e3fc-1c9c-4841-8814-e3a935fa6684"> <property name="ireport.zoom" value="1.0"/><property name="ireport.x" value="0"/><property name="ireport.y" value="229"/><style name="table"><box><pen lineWidth="1.0" lineColor="#000000"/></box></style><style name="table_TH" mode="Opaque" backcolor="#F0F8FF"><box><pen lineWidth="0.5" lineColor="#000000"/></box></style><style name="table_CH" mode="Opaque" backcolor="#BFE1FF"><box><pen lineWidth="0.5" lineColor="#000000"/></box></style><style name="table_TD" mode="Opaque" backcolor="#FFFFFF"><box><pen lineWidth="0.5" lineColor="#000000"/></box></style><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="ImageDemoDataset" uuid="47414685-b792-4a30-9b14-fc32127ad3cc">
    <parameter name="lastname" class="java.lang.String">
        <defaultValueExpression><![CDATA[$F{picture}]]></defaultValueExpression>
    </parameter>
    <parameter name="firstname" class="java.lang.String">
        <defaultValueExpression><![CDATA[$F{firstname}]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="firstname" class="java.lang.String"/>
    <field name="lastname" class="java.lang.String"/>
    <field name="email" class="java.lang.String"/>
    <field name="phone_number" class="java.lang.String"/>
    <field name="picture" class="java.lang.String"/>
    <variable name="imagePath" class="java.lang.String" resetType="None">
        <variableExpression><![CDATA[$F{picture}]]></variableExpression>
    </variable>
</subDataset>
<parameter name="ImageDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<queryString>
    <![CDATA[]]>
</queryString>
<variable name="varImage" class="java.lang.String" resetType="None"/>
<detail>
    <band height="364" splitType="Stretch">
        <componentElement>
            <reportElement x="18" y="0" width="474" height="216" uuid="771aa5d8-1470-4fd9-b062-a913b6014f19">
                <property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
            </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="ImageDemoDataset" uuid="c4a063af-d8d1-41c4-98bf-660a9eab651e">
                    <dataSourceExpression><![CDATA[$P{ImageDataSource}]]></dataSourceExpression>
                    <returnValue fromVariable="imagePath" toVariable="varImage"/>
                </datasetRun>
                <jr:column width="70" uuid="6aa96500-79ab-4b8b-aa5f-0c9dbc22638f">
                    <jr:detailCell height="30" rowSpan="1">
                        <textField>
                            <reportElement x="0" y="0" width="70" height="30" uuid="9528bbd1-627c-424c-a3f0-872f92799c94"/>
                            <textFieldExpression><![CDATA[$F{firstname}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="90" uuid="ba88fb55-d423-4d1e-99a1-b81c44f6e65a">
                    <jr:detailCell height="30" rowSpan="1">
                        <textField>
                            <reportElement x="0" y="0" width="90" height="30" uuid="5c85fd56-54ba-4d22-a3e4-adcdbbbc2b4f"/>
                            <textElement>
                                <font size="8"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{lastname} ]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="100" uuid="29524685-b68a-4906-be22-50b0acacba7d">
                    <jr:detailCell height="30" rowSpan="1">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="30" uuid="9dacce68-21ca-4bcc-b9cb-bad1b3661611"/>
                            <textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="94" uuid="4bb9635f-4173-4c5b-95ce-5a12a2332ea1">
                    <jr:detailCell height="30" rowSpan="1">
                        <textField>
                            <reportElement x="0" y="0" width="94" height="30" uuid="8c6c58b1-d3fd-4fad-995a-caaaea6afe8e"/>
                            <textFieldExpression><![CDATA[$F{phone_number}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="100" uuid="b500de9f-6abf-41b8-9e5d-d408b94d32ce">
                    <jr:detailCell height="30" rowSpan="1">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="30" uuid="0e6a1e56-974f-41a3-af08-0cf5b832b573"/>
                            <textFieldExpression><![CDATA[$F{picture}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
            </jr:table>
        </componentElement>
        <image scaleImage="FillFrame" hAlign="Center" vAlign="Middle">
            <reportElement x="48" y="266" width="100" height="98" uuid="290e1eb9-3a18-4a65-93d4-d155b98ce964">
                <printWhenExpression><![CDATA[!$V{varImage}.equalsIgnoreCase("null")]]></printWhenExpression>
            </reportElement>
            <imageExpression><![CDATA["C:\\Users\\jigpra\\Pictures\\"+$V{varImage}]]></imageExpression>
        </image>
        <staticText>
            <reportElement x="48" y="231" width="100" height="20" uuid="32fd136d-d7c4-4256-be6c-e9ad7d5f7592"/>
            <textElement>
                <font size="12"/>
            </textElement>
            <text><![CDATA[$V{varImage}]]></text>
        </staticText>
    </band>
</detail>

我想在主报告中使用表数据集字段/参数。我在表格中添加了集合数据以显示在此数据中我有一个值是每一列的图像。我想获取该图像名称(当前在集合数据集中我传递图像名称)。

如何在主报表中检索图片字段名称?

【问题讨论】:

    标签: jasper-reports


    【解决方案1】:

    您看不到图片,因为它没有正确的 Evaluation Time - 在您的情况下它需要是 Band

    staticText 不支持此类设置,您需要将其转换为textField 并为Evaluation Time 属性设置相同。

    但即使有上述建议,您仍然会看到 ImageDemoDataset subDataset 的最后一个值。如果这是你想要的,那很好。

    但是,如果您想在subDataset 中显示每条记录的图像,则必须在表格内进行。如果间距是问题,您可以创建一个columnGroup 并将图像放在groupFooter 中,将其隔离在您的主要数据下方。

    编辑:看来你想要的其实更简单,不应该涉及变量。您只需要重复相同的数据源。 这可以通过两种方式完成:

    (1)。使用原ImageDataSource参数:

    &lt;parameter name="ImageDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/&gt;

    让表 datasetRun 的表达式如下:

    &lt;dataSourceExpression&gt;&lt;![CDATA[$P{ImageDataSource}.cloneDataSource()]]&gt;&lt;/dataSourceExpression&gt;

    然后在表格下创建一个列表组件,使用与表格相同的subDataset和初始数据源表达式:

    &lt;dataSourceExpression&gt;&lt;![CDATA[$P{ImageDataSource}]]&gt;&lt;/dataSourceExpression&gt;

    并在列表组件属性的列表选项卡中将其Print Order 设置为Horizontal。确保您还指定 Cell Width 足够小以允许重复项目水平流动。

    克隆表的数据源将确保表不会耗尽它,因此列表组件仍然能够对其进行迭代。

    (2)。不要将JRBeanCollectionDataSource 作为参数传递,而是只传递您创建它的集合并为此设置一个参数。这样你就不再需要克隆数据源了:

    &lt;parameter name="ImageCollection" class="java.util.Collection"/&gt;

    然后让表 datasetRun 的表达式如下:

    &lt;dataSourceExpression&gt;&lt;![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{ImageCollection})]]&gt;&lt;/dataSourceExpression&gt;

    然后使用与表格相同的subDataset 创建一个列表,并使用上面的dataSourceExpression。您应该按照与 (1) 中相同的方式设置列表。

    【讨论】:

    • 您好@Narcis 感谢您的建议,在当前代码中我提到我在数据集字段中传递图像名称。目前我正在为静态路径工作,所以我只需要在主报告中输入表数据集的变量/字段的名称。我使用表数据集的返回值,但它不起作用。
    • 在我回答的第一部分(前三段)中,我解释了为什么它在您的情况下不起作用以及如何解决它。你试过吗?
    • 首先我尝试演示使用字符串显示图像,它的工作图像评估时间是图像/二进制,而你的图像时间是 blob。在我的情况下,我直接使用该图像的位置首先,我尝试在表格中显示图像,它使用这个评估时间(这真的很糟糕)。但现在我想在我的主报告中显示这些图像。
    • 在我的代码中,我使用变量中的字段元素在数据集运行时返回,但不幸的是它不起作用
    • 对于 JasperReports 中的元素,Evaluation Time 没有图像/二进制或 blob 值之类的东西。此外,不清楚什么有效,什么无效以及在什么情况下 - 由于标点符号使用不当,您的 cmets 几乎难以辨认。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多