【问题标题】:Jasper reports add static text row in tableJasper 报告在表格中添加静态文本行
【发布时间】:2018-09-05 02:49:54
【问题描述】:

目前我正在像这样的 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

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

【问题讨论】:

  • 要在什么条件下插入静态行?我的意思是它应该在每 X 行之后还是在输入数据源中出现某些特定值时,或者您只想将表分成两部分?
  • @cgrim 以上static row 用于启用记录,static row 以下用于禁用记录。我们需要显示表(数据库)中的所有记录。我们需要在这些记录之间放置带有static text 的单行。只有一张表而且我也有静态行的索引。只需将带有 colspan 的静态文本放在该行索引处。但不知道如何放置该静态行
  • 在这种情况下,您可以通过使用两个由静态文本分隔的单独子报表来实现。第一个子报表将过滤数据集以仅显示启用的记录,第二个子报表将过滤数据集以仅显示禁用的记录。

标签: jasper-reports


【解决方案1】:

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

<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 的情况下是相似的。首先在subDataSet中定义group

<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:columnGroupjr: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>

【讨论】:

  • 感谢您的回复。我有&lt;subDataset&gt;,而您的&lt;group&gt; 标签代码在&lt;subDataset&gt; 上不起作用,所以您知道如何在&lt;subDataset&gt; 标签中使用&lt;group&gt; 标签?
  • 因此您有一个包含一个主查询 (dataset) 的报告,并且对于每个主查询表单的项目 dataset 您有 subDataset 在这里您希望使用启用的静态文本分隔表格标志 - 正确吗?而您正在通过subReport 渲染来自subDatset 的项目或如何?
  • 我没有在 jasper 报告中使用任何查询,我可以将 JAVA 类中的对象列表作为参数传递给 jasper 报告,&lt;datasetParameterExpression&gt;&lt;![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{reportData})]]&gt;&lt;/datasetParameterExpression&gt; 使用表中的这些代码从列表中获取数据。
  • 我用subDataset 的示例更新了答案,希望对您有所帮助。
  • Report design not valid : 1. Element reaches outside table group footer contents height: y = 101, height = 21, cell available height = 21. 2. Element reaches outside table group footer contents width: x = 5, width = 548, cell available width = 548 我遇到了这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多