【问题标题】:How to make all rows in table component stretch to be of same height?如何使表格组件中的所有行都拉伸到相同的高度?
【发布时间】:2019-02-18 18:45:52
【问题描述】:

我正在使用 Jaspersoft iReport 设计器 4.7.0。我想创建一个包含 3 列的表格,表格中的每个单元格都需要拉伸到数据源的最大对象。

我尝试过更改文本元素 Position-float;stretch with overflow - true,Stretch type - 相对于最高对象的属性,但它仅适用于一行。

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

当前输出

预期输出

**使用的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>

【问题讨论】:

  • 请与数据分享报告预览
  • @Rathnayake 我已附上当前和预期的报告输出格式。请查看并建议
  • @Rathnayake 这可以实现吗?请帮忙!
  • 抱歉迟到了。 isStretchWithOverflow="true" 根据数据长度动态设置行高。所以你的格式不可能以默认方式。我对此一无所知。请参考 Petter 的回答

标签: java jasper-reports


【解决方案1】:

没有

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

AFIK 最高版本 ‎6.7.0 此答案的日期

当然你可以在 jrxml 文件中手动增加高度,但如果你想动态地做,你需要做一些事情:

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

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

粗略示例,考虑到我在汇总带中存在的表格组件上设置了 id,并且每个单元格正好有 1 个文本字段。

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);

【讨论】:

  • 感谢详细信息@Petter 让我试试上面建议的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 2011-08-25
相关资源
最近更新 更多