【问题标题】:Why is my subdataset blank when I run report from Java code? [duplicate]当我从 Java 代码运行报告时,为什么我的子数据集是空白的? [复制]
【发布时间】:2019-09-25 03:04:51
【问题描述】:

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

我在 Eclipse 中使用 Jaspersoft Studio 作为插件来生成 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);}
    }
}

【问题讨论】:

    标签: java jasper-reports


    【解决方案1】:

    由于您的 subDataset 执行查询并且您正在向它传递数据库连接。

    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
    

    在 java 中,您需要将此连接传递给 fillReport 而不是您的 JRResultSetDataSource

    print = JasperFillManager.fillReport(jasperReport,parameters, conn);
    

    JRResultSetDataSource 保留为主数据源的另一种解决方案是为子报表创建另一个数据源并将其作为参数传递,相应地修改jrxml 中的datasetRun 表达式,因此使用此数据源而不是报表连接。


    澄清一下,它在 IDE 中有效,因为在 IDE 中,您在运行报告之前选择了连接,&lt;property name="com.jaspersoft.studio.data.defaultdataadapter" value="Oracle"/&gt;,但这些设置在直接从 java 运行时无效/加载。

    【讨论】:

    • 我还需要知道,如何将结果集传递到报告中,而不是连接。
    • @alexander 我会通过连接,如果它在 IDE 中工作,它也可以在 java 中工作,如果你想使用 JRResultSetDataSource 作为回答状态,你需要为子表创建另一个并通过它在参数中。然后编辑数据源表达式以使用此参数(数据源)而不是连接。
    • 感谢您的回复。但我不知道该怎么做。请分享上述解决方案的代码sn-p
    • @alexander 1. 在 java 中创建数据源,2 传入参数映射,3 在 jrxml 中创建具有正确类的参数,4 将其传递给 datasetRun(数据源表达式)。如果您在某些特定步骤遇到问题,请告诉我。
    • 我需要将结果集从 java 传递给 jasper 报告,而不是(报告中存在查询)。怎么做。我在 java 中创建了 oracle 数据源。我不知道如何传递参数映射..和其他后续步骤请帮助@Petter
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 2016-12-07
    • 2021-10-23
    • 1970-01-01
    相关资源
    最近更新 更多