为什么当我从Java代码运行报告时,我的子数据集是空的?

为什么当我从Java代码运行报告时,我的子数据集是空的?,java,jasper-reports,Java,Jasper Reports,我使用Jaspersoft Studio创建了一个表格设计。在Java中使用此jrxml时,不显示任何表 我正在使用Jaspersoft Studio作为Eclipse中的插件来生成jrxml。预览表时会在JSS中显示,但当我使用Java代码将其导出为pdf/xls/docx时,它不会被加载。没有任何数据的空文件 这是我的JRXML文件 <?xml version="1.0" encoding="UTF-8"?> <jasperReport xmlns="http://jasp

我使用Jaspersoft Studio创建了一个表格设计。在Java中使用此jrxml时,不显示任何表

我正在使用Jaspersoft Studio作为Eclipse中的插件来生成jrxml。预览表时会在JSS中显示,但当我使用Java代码将其导出为pdf/xls/docx时,它不会被加载。没有任何数据的空文件

这是我的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="Blank_A4_Landscape" pageWidth="802" pageHeight="555" orientation="Landscape" columnWidth="802" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="f391983e-5ad3-4a64-b310-fc7f3426e3f0">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Oracle"/>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
            <style backcolor="#E3F1FF"/>
        </conditionalStyle>
    </style>
    <subDataset name="table" uuid="9fa2cf87-1e9b-4673-a13c-55f4b337d4db">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Oracle"/>
        <queryString language="SQL">
            <![CDATA[select * from CODE]]>
        </queryString>
        <field name="NAME" class="java.lang.String"/>
        <field name="MOBILE" class="java.lang.String"/>
        <field name="COMPANY_NAME" class="java.lang.String"/>
        <field name="SITE_ID" class="java.lang.String"/>
        <field name="SITE_NAME" class="java.lang.String"/>
        <field name="IN_TIME" class="java.lang.String"/>
        <field name="OUT_TIME" class="java.lang.String"/>
        <field name="CREATE_ON" class="java.lang.String"/>
    </subDataset>
    <queryString language="SQL">
        <![CDATA[select * from CODE]]>
    </queryString>
    <field name="NAME" class="java.lang.String"/>
    <field name="MOBILE" class="java.lang.String"/>
    <field name="COMPANY_NAME" class="java.lang.String"/>
    <field name="SITE_ID" class="java.lang.String"/>
    <field name="SITE_NAME" class="java.lang.String"/>
    <field name="IN_TIME" class="java.lang.String"/>
    <field name="OUT_TIME" class="java.lang.String"/>
    <field name="CREATE_ON" class="java.lang.String"/>
    <title>
        <band height="50" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="20" width="100" height="30" forecolor="#007BFF" uuid="46c45769-ac34-41af-bdb4-c45729f333b9"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="true"/>
                </textElement>
                <text><![CDATA[Call Logs]]></text>
            </staticText>
            <textField pattern="MMM d, yyyy h:mm:ss a">
                <reportElement x="450" y="20" width="330" height="30" forecolor="#007BFF" uuid="7a3c6c9d-bf5b-487d-8e13-720ecda75b61"/>
                <textElement textAlignment="Right" verticalAlignment="Middle">
                    <font size="12" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Report Generated on:  "+new java.util.Date()]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <detail>
        <band height="243" splitType="Stretch">
            <printWhenExpression><![CDATA[$V{REPORT_COUNT} != 0]]></printWhenExpression>
            <componentElement>
                <reportElement x="41" y="24" width="720" height="200" uuid="24f1ba3d-2de9-4a32-9f9f-75d592f91359">
                    <printWhenExpression><![CDATA[$V{REPORT_COUNT} == 1]]></printWhenExpression>
                </reportElement>
                <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" uuid="f76f4c9b-3345-4d0e-8cad-dd8a75913665">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:column width="104" uuid="229daaed-80a6-42c5-9ade-2f0c75d1963e">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="104" height="30" uuid="cfa9503a-bbfe-41fb-96a6-46c633a691fc"/>
                                <text><![CDATA[NAME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="104" height="30" uuid="d0395260-510c-44dd-88c0-3c5ab97a6f65"/>
                                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="101" uuid="94785e2c-8fa8-4402-8506-4f234bf064bd">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="101" height="30" uuid="03b5ef55-113a-485c-9d09-519dcc9d6152"/>
                                <text><![CDATA[MOBILE]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="101" height="30" uuid="dba6ea8d-23d1-4af3-bf64-475a5266b85f"/>
                                <textFieldExpression><![CDATA[$F{MOBILE}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="101" uuid="8151f7e4-e41b-430b-8730-b5499a6d5d1f">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="101" height="30" uuid="2321d1fa-4e3f-4d3f-8727-bae188bafd7b"/>
                                <text><![CDATA[COMPANY_NAME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="101" height="30" uuid="d9d2d45e-98cc-4f52-8b3d-4d2829d5df34"/>
                                <textFieldExpression><![CDATA[$F{COMPANY_NAME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="101" uuid="1c6cc322-6497-437a-8945-c1c3f08287b0">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="101" height="30" uuid="2720a693-f022-4498-93e3-4d0aa7b37982"/>
                                <text><![CDATA[SITE_NAME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="101" height="30" uuid="c34d16ae-244f-4b73-8e54-71655a5fc231"/>
                                <textFieldExpression><![CDATA[$F{SITE_NAME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="105" uuid="7b35eec3-38c2-4b76-aa58-d7cb1dc0925c">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="105" height="30" uuid="29e866cb-fecc-4854-abdb-4e131883b68e"/>
                                <text><![CDATA[IN_TIME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="105" height="30" uuid="d3b432d0-95e4-4d4a-b6a8-4984f3c95164"/>
                                <textFieldExpression><![CDATA[$F{IN_TIME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="104" uuid="ba443c16-d86f-483c-9e79-035cfc42ce40">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="104" height="30" uuid="a6d98a0b-d280-475a-8b7e-7e88778c5dcd"/>
                                <text><![CDATA[OUT_TIME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="104" height="30" uuid="9fa9cd8c-da08-4871-9cb1-6655d0a14e34"/>
                                <textFieldExpression><![CDATA[$F{OUT_TIME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="104" uuid="3aac9567-fd44-4954-8542-60806e43e960">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="104" height="30" uuid="81d7af3f-bfd5-4bda-8c4c-645d44c718b9"/>
                                <text><![CDATA[CREATE_ON]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="104" height="30" uuid="502689db-da95-4dba-bdd9-fc066be71100"/>
                                <textFieldExpression><![CDATA[$F{CREATE_ON}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

这是我的Java代码:

public class PDFReportGenerator {
    public static String ACTUAL_REPORT_PATH = "E:\\path\\Blank_A4_Landscape.jrxml";
    public static String PDF_PATH = "E:\\path\\Blank_A4_Landscape.pdf";

    public static void main(String[] args) throws JRException, ClassNotFoundException, SQLException{
        generateReport();
    }

    static void generateReport() {
        Connection conn = null;
        ResultSet rs = null;
        JasperReport jasperReport = null;
        JasperPrint print = null;
        String query ="SELECT * FROM CODE";
        try {   
            if(conn==null) {
                try {
                    conn= OracleConnection.getConnection();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }   
        CallableStatement cstmt = conn.prepareCall(query);
        rs=cstmt.executeQuery();
        JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);      
        jasperReport = JasperCompileManager.compileReport(ACTUAL_REPORT_PATH);
        Map <String, Object> parameters = new HashMap <String, Object>();
        print = JasperFillManager.fillReport(jasperReport, parameters, resultSetDataSource);
        JasperExportManager.exportReportToPdfFile(print, PDF_PATH);
        } catch(Exception e) {
            System.out.println(e);}
    }
}
公共类PDFReportGenerator{
公共静态字符串ACTUAL\u REPORT\u PATH=“E:\\PATH\\Blank\u A4\u scape.jrxml”;
公共静态字符串PDF\u PATH=“E:\\PATH\\Blank\u A4\u scape.PDF”;
公共静态void main(字符串[]args)引发JRException、ClassNotFoundException、SQLException{
generateReport();
}
静态void生成器报告(){
连接conn=null;
结果集rs=null;
JasperReport-JasperReport=null;
JasperPrint print=null;
String query=“从代码中选择*”;
试试{
如果(conn==null){
试一试{
conn=OracleConnection.getConnection();
}捕获(例外情况除外){
例如printStackTrace();
}
}   
CallableStatement cstmt=conn.prepareCall(查询);
rs=cstmt.executeQuery();
JResultSetDataSource resultSetDataSource=新的JResultSetDataSource(rs);
jasperReport=jaspecompilemanager.compileReport(实际报告路径);
Map参数=newhashmap();
print=JasperFillManager.fillReport(jasperReport、参数、结果数据源);
JasperExportManager.exportReportToPdfFile(打印,PDF_路径);
}捕获(例外e){
System.out.println(e);}
}
}

因为您的
子数据集执行查询,并且您正在向其传递数据库连接

<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
另一种将
jresultsetdatasource
作为主数据源的解决方案是为子报表创建另一个数据源,并将其作为参数传递,从而相应地修改jrxml中的
datasetRun
表达式,从而使用此数据源而不是报表连接


为了澄清,它在IDE中工作,因为在IDE中,您在运行报告之前选择了连接,
,但是
print = JasperFillManager.fillReport(jasperReport,parameters, conn);