如果我有这样的表:
ID | details
___________________
1 | A
2 | B
3 | C
4 | D
5 | E
6 | F
6 | G
我希望像这样在jasper中显示表:
ID | details
___________________
1 | A
2 | B
3 | C
4 | D
5 | E
6 | F,G
如果细节具有相同的id,则细节单元格被合并
我可以使用什么样的表达式来实现jasper(不在查询中)?我正在使用jasper 4.5
答案 0 :(得分:1)
这可以通过$F{ID}
上的分组来实现,使用JRAbstractScriptlet连接$F{detail}
字符串并在groupFooter
中显示结果带
Scriptlet
的示例public class Scriptlet extends JRDefaultScriptlet {
public void afterDetailEval() throws JRScriptletException
{
String details = (String)this.getVariableValue("detailsWithId");
String detail = (String)this.getFieldValue("detail");
StringBuffer sbuffer = new StringBuffer();
if (details != null)
{
sbuffer.append(details);
sbuffer.append(", ");
}
sbuffer.append(detail);
this.setVariableValue("detailsWithId", sbuffer.toString());
}
}
jrxml 的示例,报告,请注意scriptletClass="Scriptlet"
标记中的jasperReport
(类Scriptlet
需要在类路径中)
<?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="Example2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" scriptletClass="Scriptlet" uuid="ca579c38-1e4f-4993-a020-efcea9d1096e">
<queryString language="xPath">
<![CDATA[/report/entry]]>
</queryString>
<field name="id" class="java.lang.String">
<fieldDescription><![CDATA[id]]></fieldDescription>
</field>
<field name="detail" class="java.lang.String">
<fieldDescription><![CDATA[detail]]></fieldDescription>
</field>
<variable name="detailsWithId" class="java.lang.String" resetType="Group" resetGroup="id" calculation="System">
<variableExpression><![CDATA[]]></variableExpression>
</variable>
<group name="id">
<groupExpression><![CDATA[$F{id}]]></groupExpression>
<groupFooter>
<band height="20">
<textField>
<reportElement x="100" y="0" width="100" height="20" uuid="19ec4996-dee5-461e-bc61-2bf967632a9e"/>
<textFieldExpression><![CDATA[$V{detailsWithId}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="f0f77e03-d040-4628-94b3-e1506e713399"/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
</band>
</groupFooter>
</group>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20" uuid="bee3aa66-130d-4f05-8bb2-55e57c068ecc"/>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20" uuid="9b4d86d1-4904-4854-b723-6fe62df170ce"/>
<text><![CDATA[details]]></text>
</staticText>
</band>
</columnHeader>
</jasperReport>
数据源
的示例<report>
<entry><id>1</id><detail>A</detail></entry>
<entry><id>2</id><detail>B</detail></entry>
<entry><id>3</id><detail>C</detail></entry>
<entry><id>4</id><detail>D</detail></entry>
<entry><id>5</id><detail>E</detail></entry>
<entry><id>6</id><detail>F</detail></entry>
<entry><id>6</id><detail>G</detail></entry>
</report>
<强>结果强>: