Jasper报告:表组件中的CSV DataSource

时间:2013-05-24 18:40:12

标签: jasper-reports

我有一个要求,我必须阅读CSV文件并在Jasper中准备一份报告。我可以在常规文本字段中使用CSV数据源。但是当我向Detail或Summary部分添加一个表时,它需要创建一个新的数据集。我成功创建了它,但是当报告运行时,它无法从CSV数据源获取数据,我收到一个空白报告。如果我使用数据库而不是CSV,同样的事情也有效。 Jasper有没有错误。

3 个答案:

答案 0 :(得分:0)

对于表组件,您必须创建新数据源,您必须执行以下操作:拖动表组件并单击“新建数据集”,然后从连接/数据源下拉列表中选择CSV数据集。

答案 1 :(得分:0)

我遇到了同样的问题。要解决此问题,请转到表组件,右键单击,然后选择"编辑数据源",选择"连接数据源表达式"并写:

((net.sf.jasperreports.engine.data.JRCsvDataSource)$P{REPORT_DATA_SOURCE})   

答案 2 :(得分:0)

下面是一个示例JRXML,它显示了如何在表组件中使用CSV数据源。

请注意,您需要在子数据集中命名字段" COLUMN _" +列索引。这样在JRCsvDataSource#getFieldValue中,您的字段将被正确定位,而无需在CSV文件中为列标题添加行。

    <?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.0.final using JasperReports Library version 6.3.0  -->
<!-- 2016-08-23T13:06:06 -->
<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="SimpleReportWithTable" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b32b572d-c517-48ba-a44e-3402966e9932">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="SimpleCsvDataAdapter"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <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_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="#FBFDFF"/>
        </conditionalStyle>
    </style>
    <subDataset name="Dataset1" uuid="76efa803-c263-4538-a39a-8ddb1712e58a">
        <field name="COLUMN_0" class="java.lang.String"/>
        <field name="COLUMN_1" class="java.lang.String"/>
        <field name="COLUMN_2" class="java.lang.String"/>
        <field name="COLUMN_3" class="java.lang.String"/>
        <field name="COLUMN_4" class="java.lang.String"/>
        <field name="COLUMN_5" class="java.lang.String"/>
        <field name="COLUMN_6" class="java.lang.String"/>
        <field name="COLUMN_7" class="java.lang.String"/>
        <field name="COLUMN_8" class="java.lang.String"/>
    </subDataset>
    <parameter name="CSV_DATA_SOURCE" class="net.sf.jasperreports.engine.data.JRCsvDataSource" isForPrompting="false">
        <defaultValueExpression><![CDATA[new net.sf.jasperreports.engine.data.JRCsvDataSource("path_to_csv_file")]]></defaultValueExpression>
    </parameter>
    <summary>
        <band height="70" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="10" width="200" height="60" uuid="a5c67e1f-1b96-4044-b4d2-ba9da8502bab">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
                    <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
                    <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
                    <property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
                </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" whenNoDataType="AllSectionsNoDetail">
                    <datasetRun subDataset="Dataset1" uuid="7738bf3c-ecda-4864-9804-c6319de53296">
                        <dataSourceExpression><![CDATA[$P{CSV_DATA_SOURCE}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="66" uuid="5c06dacd-68b4-4972-87e9-45dcbf181cbd">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="66" height="30" uuid="3ecb3cf8-02f5-4783-b407-9d36a3b76a9e"/>
                                <text><![CDATA[Row 1]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="66" height="30" uuid="21db9e1b-c129-40f6-afd5-c8fc9b939112"/>
                                <textFieldExpression><![CDATA[$F{COLUMN_0}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="66" uuid="45989e75-9ad1-4a48-ae63-ddaf380930e7">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="66" height="30" uuid="392ead1f-fd67-431c-8da6-3cf3dad9728d"/>
                                <text><![CDATA[Row 2]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="66" height="30" uuid="1a1fb7ba-cec2-44f7-bdc7-3a3887302fb0"/>
                                <textFieldExpression><![CDATA[$F{COLUMN_1}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="66" uuid="128a9867-c221-4862-a9ac-f5cc99e394f1">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="66" height="30" uuid="033b57f1-b3dd-4909-8ed7-84c99f92eccd"/>
                                <text><![CDATA[Row 3]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="66" height="30" uuid="0d2027c4-e761-4e14-b093-31be3c0493e0"/>
                                <textFieldExpression><![CDATA[$F{COLUMN_2}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </summary>
</jasperReport>