主要关注的是我的查询在sql和表数据集查询预览中是否运行顺利。我是说在表数据集查询数据预览中,它显示了我的数据库表中的2条记录。
请找到屏幕截图。
还可以找到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="report2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <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="Table Dataset 1">
        <queryString>
            <![CDATA[select lname, fname from test1
order by fname]]>
        </queryString>
        <field name="lname" class="java.lang.String"/>
        <field name="fname" class="java.lang.String"/>
    </subDataset>
    <subDataset name="New Dataset 1">
        <queryString language="SQL">
            <![CDATA[select lname, fname from test1
order by fname]]>
        </queryString>
        <field name="lname" class="java.lang.String"/>
        <field name="fname" class="java.lang.String"/>
    </subDataset>
    <queryString>
        <![CDATA[select lname, fname from test1
order by fname]]>
    </queryString>
    <field name="lname" class="java.lang.String"/>
    <field name="fname" 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">
            <staticText>
                <reportElement x="100" y="41" width="102" height="20"/>
                <textElement/>
                <text><![CDATA[lname]]></text>
            </staticText>
            <staticText>
                <reportElement x="202" y="41" width="147" height="20"/>
                <textElement/>
                <text><![CDATA[fname]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <componentElement>
                <reportElement key="table" style="table" x="100" y="0" width="360" height="86"/>
                <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">
                        <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="90">
                        <jr:detailCell style="table_TD" height="20" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression class="java.lang.String"><![CDATA[$F{lname}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="90">
                        <jr:detailCell style="table_TD" height="20" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression class="java.lang.String"><![CDATA[$F{fname}]]></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>

但pdf报告的输出只有一条记录,最后一条是1。

最佳答案

在使用jrbeancollectiondatasource时发生这种情况。我知道一个事实,所有的数据都在那里,但是表工具总是丢失第一行。
看起来主报表对集合或其他东西执行“.next()”,然后子报表(也称为表)继续处理其余数据
无论如何,我解决了这个问题,将$p{report_data_source}.clonedatasource()作为表的数据源表达式。然后,看起来该表以一个全新的数据源开始,并可以遍历所有项,因此,它工作正常。
我不知道您使用的是什么数据源,但在使用子报表之前,您必须找到一种将光标移动到数据开头的方法。
综上所述,我在jrxml文件中这样做了:
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}.cloneDataSource()]]></dataSourceExpression>
但只是因为我用的是JRBeanCollectionDataSource

10-04 22:55
查看更多