首页 > 解决方案 > Jasper 报告在表格中添加静态文本行

问题描述

目前我正在像这样的碧玉报告中显示表格

Col1   |  Col2   |  Col3   |  Col4   
------------------------------------
Row1C1 | Row1C2  | Row1C3  | Row1C4
Row2C1 | Row2C2  | Row2C3  | Row2C4
Row3C1 | Row3C2  | Row3C3  | Row3C4
Row4C1 | Row4C2  | Row4C3  | Row4C4

但现在我想做这样的桌子

Col1   |  Col2   |  Col3   |  Col4   
------------------------------------
Row1C1 | Row1C2  | Row1C3  | Row1C4
Row2C1 | Row2C2  | Row2C3  | Row2C4
Added static row here with colspan
Row3C1 | Row3C2  | Row3C3  | Row3C4
Row4C1 | Row4C2  | Row4C3  | Row4C4

有可能吗?如果是,那么我如何在表中添加静态行?

标签: jasper-reports

解决方案


您可以使用数据组来做到这一点。这是一个如何实现的示例:

<queryString language="SQL">
    <![CDATA[SELECT col1, col2, col3 FROM some_table ORDER BY flag]]>
</queryString>
<field name="col1" class="java.lang.Integer">
    <property name="com.jaspersoft.studio.field.label" value="col1"/>
    <property name="com.jaspersoft.studio.field.tree.path" value="some_table"/>
</field>
<field name="col2" class="java.lang.String">
    <property name="com.jaspersoft.studio.field.label" value="col2"/>
    <property name="com.jaspersoft.studio.field.tree.path" value="some_table"/>
</field>
<field name="flag" class="java.lang.Bolean">
    <property name="com.jaspersoft.studio.field.label" value="flag"/>
    <property name="com.jaspersoft.studio.field.tree.path" value="some_table"/>
</field>
<group name="flagGroup">
    <groupExpression><![CDATA[$F{flag}]]></groupExpression>
    <groupFooter>
        <band height="39">
            <printWhenExpression><![CDATA[$F{flag}]]></printWhenExpression>
            <staticText>
                <reportElement x="0" y="10" width="380" height="20" uuid="d4d760c5-0587-4bd7-973a-de1d9b0c8da6"/>
                <text><![CDATA[Any static text to separate groups]]></text>
            </staticText>
            <line>
                <reportElement x="0" y="0" width="330" height="1" uuid="b00741f6-3d2c-4cca-a3fe-397aa7fbf906"/>
            </line>
            <line>
                <reportElement x="0" y="30" width="330" height="1" uuid="0f30d1bc-30ef-48e9-b40e-ab4a3e2b1ffc"/>
            </line>
        </band>
    </groupFooter>
</group>

它将使用flag字段对行进行分组,您可以在 中插入所需的内容groupFooter,例如staticTextlinerectangle

这是预览:

使用数据组的预览

您可以在 JasperSoft Wiki 上找到有关群组的更多信息:https ://community.jaspersoft.com/wiki/groups


更新:在使用的情况下subDatasetjr:table它是相似的。首先定义groupsubDataSet

<subDataset name="Dataset1" uuid="bb794b3a-5505-492a-8b9f-500b274a8334">
    <field name="col1" class="java.lang.Integer">
        <fieldDescription><![CDATA[col1]]></fieldDescription>
    </field>
    <field name="col2" class="java.lang.String">
        <fieldDescription><![CDATA[col2]]></fieldDescription>
    </field>
    <field name="col3" class="java.lang.String">
        <fieldDescription><![CDATA[col3]]></fieldDescription>
    </field>
    <field name="flag" class="java.lang.Boolean">
        <fieldDescription><![CDATA[flag]]></fieldDescription>
    </field>
    <group name="flagGroup">
        <groupExpression><![CDATA[$F{flag}]]></groupExpression>
    </group>
</subDataset>

jr:columnGroup然后在of 中添加组页脚(或页眉)定义jr:table

<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="0e226f43-fb2c-43df-baa8-4cf91b3e0523">
        <datasetParameter name="REPORT_DATA_SOURCE">
            <datasetParameterExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{reportData})]]></datasetParameterExpression>
        </datasetParameter>
    </datasetRun>
    <jr:columnGroup width="210" uuid="1b2e36c1-0c14-48a0-97b8-66fa4330a71a">
        <jr:groupFooter groupName="flagGroup">
            <jr:cell style="Table_CH" height="30" rowSpan="1">
                <staticText>
                    <reportElement x="0" y="0" width="210" height="30" uuid="840fd833-5895-47d0-b63f-dd058a936a8a">
                        <printWhenExpression><![CDATA[$F{flag}]]></printWhenExpression>
                    </reportElement>
                    <text><![CDATA[Any static text to separate groups]]></text>
                </staticText>
            </jr:cell>
        </jr:groupFooter>
...
    </jr:columnGroup>
</jr:table>

推荐阅读