【问题标题】:Why do jasper reports generate multiple tables instead of one?为什么 jasper 报告会生成多个表而不是一个表?
【发布时间】:2016-05-25 14:41:20
【问题描述】:

我想使用 jasperreport 生成带有表格的 pdf 文件,我的 java 代码如下:

JasperReport report = JasperCompileManager.compileReport("src/main/resources/report1.jrxml");
Map<String,Object> params = new HashMap<String,Object>();
List<Info> infos = new ArrayList<Info>();
for(int i=0;i<10;i++) {
    Info info = new Info();
    info.setAccountCode("abc");
    info.setAccountName("test");
    infos.add(info);                
}

JRBeanCollectionDataSource beans = new JRBeanCollectionDataSource(infos);
params.put("tableSource", infos);

JasperPrint print = JasperFillManager.fillReport(report , params, beans);
JasperExportManager.exportReportToPdfFile(print,"jasper.pdf");

在jrxml文件中,我创建了一个参数tableSource(它的类型是List),添加了两个字段accountNameaccountCode,添加了一个'Table'报表元素,也创建了

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{tableSource})

在表属性中,完整的 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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="6fa4b189-7e18-456b-9307-710a8d6972c1">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="20"/>
    <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>
    <style name="table 1">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Table Dataset 1" uuid="6d5cf195-8b52-4dfe-9f53-9e3eb5be5db3">
        <field name="accountCode" class="java.lang.String"/>
        <field name="accountName" class="java.lang.String"/>
    </subDataset>
    <parameter name="tableSource" class="java.util.List"/>
    <field name="accountCode" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <componentElement>
                <reportElement key="table 1" style="table 1" x="108" y="30" width="450" height="50" uuid="c7628262-8e54-4c62-b977-175f6aea984e"/>
                <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="Table Dataset 1" uuid="8beea930-93ae-4fa8-826b-cebfebef3a86">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{tableSource})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="90" uuid="0155325f-f82b-4b89-849c-dff724c771a9">
                        <jr:tableHeader style="table 1_TH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="90" height="30" uuid="ea43a2ac-624e-4c44-a53c-60f79a2a4cd3"/>
                                <text><![CDATA[Account Code]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell style="table 1_TD" height="30" rowSpan="1">
                            <textField isStretchWithOverflow="true">
                                <reportElement x="0" y="0" width="90" height="20" uuid="28ac521c-37bf-4cfc-80ad-35a380e32d9a"/>
                                <textElement>
                                    <font fontName="Arial" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
                                </textElement>
                                <textFieldExpression><![CDATA[$F{accountCode}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="90" uuid="9249d41a-f7af-4abf-9163-0a15d0a621a8">
                        <jr:tableHeader style="table 1_TH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="90" height="30" uuid="81274afc-cb28-4251-8e9d-8ffe2466d2dc"/>
                                <text><![CDATA[Account Name]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell style="table 1_TD" height="30" rowSpan="1">
                            <textField isStretchWithOverflow="true">
                                <reportElement x="0" y="0" width="90" height="20" uuid="5876bb0e-cc9d-4d34-a2ba-483016846752"/>
                                <textElement>
                                    <font fontName="Arial" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
                                </textElement>
                                <textFieldExpression><![CDATA[$F{accountName}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

我希望它会生成一张像下一张这样的表:

Account Code  Account Name
abc           test
abc           test

然而,真正的结果是生成了 2 个表格,如下所示:

Account Code  Account Name
abc           test
abc           test

Account Code  Account Name
abc           test
abc           test

为什么会生成两个相同的表?

我正在使用 iReport 5.6.0,jasperreport 6.2.2

【问题讨论】:

  • 您在主报告(详细带)中有 2 轮迭代
  • 将新的 JREmptyDataSource(1) 传递给报表。
  • @PetterFriberg,谢谢你的提示,它有效,请问为什么我应该传递 JREmptyDataSource 而不是 JRCollectionDataSource?这是否意味着如果我在 jrxml 中使用 Table 组件,我总是传递 JREmptyDataSource?
  • @frank,我已经发布了一个答案来解释原因

标签: java jasper-reports


【解决方案1】:

您应该将带有 1 条记录的 JREmptyDataSource 传递给 JasperFillManager

JasperPrint print = JasperFillManager.fillReport(report , params, new JREmptyDataSource(1));

为什么?

Jasper Reports 在您传递给JasperFillManager 的数据源上迭代detail 波段,因此它将创建与JRBeanCollectionDataSource beans 中的记录一样多的详细波段。在您的情况下,您不希望这样做,因为您将此数据源作为&lt;jr:table&gt; 组件使用的参数传递。您的设置是使用 jr:table 显示详细信息带一次(1 条记录),该表在作为参数传递的数据源上进行迭代,获取 1 条记录的最简单方法是 new JREmptyDataSource(1)

【讨论】:

    【解决方案2】:

    您已将您的Table 放入Details 乐队中。

    为结果中的每一行生成Details 带。由于您有两行数据,因此您还获得了两个表。

    尝试将 Table 移动到另一个频段,例如 SummaryColumn Footer 或在您的情况下有意义的。

    【讨论】:

    • 谢谢您的回答,我尝试将表格移动到其他部分,但有时会发生溢出,例如当数据列表大小为 20
    • 溢出?我以为你有两张桌子?
    • 只有一张表,如果我的数据列表大小为10,则正确,但如果我的数据列表大小为20,则抛出溢出异常。
    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    相关资源
    最近更新 更多