JasperReport表头的stretchType属性无效的解决



JasperReport表头的stretchType属性无效的解决

受JasperReport 根据内容动态调整报表行高启发:
1.Stretch with overflow 针对TextFileld
2.Stretch Type:relative to tallest object/relative to band height
3.Position type:Float/Fix relative to top/Fix relative to bottom
4.element group(为元素分组)

问题:
表格column多并且表头字数多的时候发现表头不会自动拉伸,打开layout的.jrxml文件,发现表头是使用staticText作为表头文字描述的:(往往表头都是写成静态文本的)
columnHeader节点–>band节点–>

Xml代码 收藏代码
<staticText>
<reportElement mode=”Opaque” backcolor=”#CCCCCC” x=”2″
y=”0″ width=”53″ height=”15″ key=”staticText-1″ stretchType=”RelativeToBandHeight”>
<printWhenExpression><![CDATA[$V{PAGE_NUMBER}.intValue() <= 1]]></printWhenExpression>
</reportElement>
<box topBorder=”Thin” topBorderColor=”#000000″ leftBorder=”Thin”
leftBorderColor=”#000000″ rightBorder=”Thin” rightBorderColor=”#000000″
bottomBorder=”Thin” bottomBorderColor=”#000000″ />
<text><![CDATA[This is the Head of Table]]></text>
</staticText>

 

尽管这里在reportElement节点加入了stretchType=”RelativeToBandHeight”属性,但是发现表头并不能自动拉伸。 看过上面文章之后知道,对于staticText的块,这个属性是不起作用的。

解决:

修改为:

Xml代码 收藏代码
<textField isStretchWithOverflow=”true”>
<reportElement mode=”Opaque” backcolor=”#CCCCCC” x=”55″
y=”0″ width=”53″ height=”15″ key=”textField-2″ stretchType=”RelativeToBandHeight”
positionType=”Float”>
<printWhenExpression><![CDATA[$V{PAGE_NUMBER}.intValue() <= 1]]></printWhenExpression>
</reportElement>
<box topBorder=”Thin” topBorderColor=”#000000″ leftBorder=”None”
leftBorderColor=”#000000″ rightBorder=”Thin” rightBorderColor=”#000000″
bottomBorder=”Thin” bottomBorderColor=”#000000″ />
<textElement textAlignment=”Center” verticalAlignment=”Middle”>
<font fontName=”Arial” size=”9″ isBold=”false” pdfFontName=”Helvetica”
isPdfEmbedded=”false” pdfEncoding=”Cp1252″ />
</textElement>
<textFieldExpression class=”java.lang.String”><![CDATA["This is the Head of Table"]]></textFieldExpression>
</textField>
即可,注意除了把staticText修改为textField 外,
要加入isStretchWithOverflow=”true”属性,
另外textFieldExpression 使用双引号修饰原本的static text值。

回头在官网上找一下相关资料印证。另外这个 positionType=”Float”属性可能也会起作用,见http://stackoverflow.com/questions/9377392/how-to-stretch-textfield-in-a-jasperreport-as-per-dynamic-content