如何使表格组件中的所有行都伸展到相同的高度?

时间:2019-02-15 19:15:09

标签: java jasper-reports

我正在使用Jaspersoft iReport Designer 4.7.0。我想创建一个具有3列的表,并且该表中的每个单元格都需要扩展到数据源的最大对象。

我尝试过更改文本元素Position-float的属性;使用溢出进行拉伸-true,Stretch类型-相对于最高的对象,但仅适用于一行。

我希望它对表格的所有行有效。

当前输出

CurrentOutput

预期产量

expectedOutputwithEachCellSpacing

**使用了jrxml *

   <?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="report3" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f32dfce1-c0d4-4f20-b896-653e7497d7ba">
        <property name="ireport.zoom" value="1.0"/>
        <property name="ireport.x" value="0"/>
        <property name="ireport.y" value="0"/>
        <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>
        <subDataset name="tabeDataSet" uuid="46e73a3d-e883-4c74-a062-6984cc6dac58">
            <field name="id" class="java.lang.String"/>
            <field name="city" class="java.lang.String"/>
            <field name="street" class="java.lang.String"/>
        </subDataset>
        <subDataset name="Table Dataset 1" uuid="4a76bbab-d785-49f2-82aa-705ea0a2e0d1"/>
        <subDataset name="Table Dataset 2" uuid="984926ca-9b74-4713-a4a8-25e7e3a32697"/>
        <parameter name="tableData" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
        <detail>
            <band height="191">
                <componentElement>
                    <reportElement uuid="6e73c624-495f-4558-bfe4-65a0487825ab" key="table 1" x="74" y="49" width="360" height="52"/>
                    <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="tabeDataSet" uuid="dcaeff11-66ee-47b5-af06-d2f183cb5830">
                            <dataSourceExpression><![CDATA[$P{tableData}]]></dataSourceExpression>
                        </datasetRun>
                        <jr:column uuid="ad308669-ebfb-43ca-b518-326779bbb20e" width="90">
                            <jr:detailCell height="20">
                                <textField isStretchWithOverflow="true">
                                    <reportElement uuid="34fe3944-c103-4c85-b509-22df1b589d11" positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="90" height="20"/>
                                    <box>
                                        <pen lineWidth="0.5"/>
                                        <topPen lineWidth="0.5"/>
                                        <leftPen lineWidth="0.5"/>
                                        <bottomPen lineWidth="0.5"/>
                                        <rightPen lineWidth="0.5"/>
                                    </box>
                                    <textElement/>
                                    <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column uuid="0dbf249d-ee6e-4431-bba2-6d13b610582f" width="90">
                            <jr:detailCell height="20">
                                <textField isStretchWithOverflow="true">
                                    <reportElement uuid="886ba9a8-98bc-43cf-9abd-c6fd0410029a" positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="90" height="20"/>
                                    <box>
                                        <pen lineWidth="0.5"/>
                                        <topPen lineWidth="0.5"/>
                                        <leftPen lineWidth="0.5"/>
                                        <bottomPen lineWidth="0.5"/>
                                        <rightPen lineWidth="0.5"/>
                                    </box>
                                    <textElement/>
                                    <textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column uuid="edbd158e-278a-4a5d-98ab-4ea592001ee9" width="90">
                            <jr:detailCell height="20">
                                <textField isStretchWithOverflow="true">
                                    <reportElement uuid="59ec1fc2-0a76-4c4c-96b0-7e2b5fbad3df" positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="90" height="20"/>
                                    <box>
                                        <pen lineWidth="0.5"/>
                                        <topPen lineWidth="0.5"/>
                                        <leftPen lineWidth="0.5"/>
                                        <bottomPen lineWidth="0.5"/>
                                        <rightPen lineWidth="0.5"/>
                                    </box>
                                    <textElement/>
                                    <textFieldExpression><![CDATA[$F{street}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                    </jr:table>
                </componentElement>
            </band>
        </detail>
    </jasperReport>

1 个答案:

答案 0 :(得分:1)

您没有可以设置的属性来实现在拉伸时所有行的大小相同。

AFIK最新版本此答案的日期6.7.0

自然地,您可以手动增加jrxml文件中的高度,但是如果您想动态地增加高度,则需要执行以下操作:

  1. 使用字体度量标准来计算需要在Java中循环数据集以查找文本的最大高度的高度,请参见this answer以了解如何使用字体度量标准。

  2. 将jrxml加载为JasperDesign对象,然后设置新高度。

原始示例,考虑到我已经在摘要区域中的表格组件上设置了id,并且每个单元格都只有1个textField。

int newHeight = 40;
//Load into desgin  
JasperDesign design = JRXmlLoader.load("path/to/my/report.jrxml");
JRDesignBand summary = (JRDesignBand) design.getSummary();
//I have set id on my component in jrxml so that I find it quickly
JRDesignComponentElement tblElement = (JRDesignComponentElement) summary.getElementByKey("tbl");
StandardTable tbl = (StandardTable) tblElement.getComponent();
//loop all columns and set new height to both cell and textField
for (BaseColumn bc : tbl.getColumns()) {
    StandardColumn col = (StandardColumn) bc;
    DesignCell cell = (DesignCell) col.getDetailCell();
    JRDesignTextField tf = (JRDesignTextField) cell.getChildren().get(0);
    cell.setHeight(newHeight);
    tf.setHeight(newHeight);
}
//Get my report from design and later fill it.
JasperReport report = JasperCompileManager.compileReport(design);