不同的csv文件作为JasperReports中的数据源

时间:2017-11-06 10:18:38

标签: csv jasper-reports

我试图在Jaspersoft Studio中生成报告。我的目标是在一个报告中使用两个不同的CSV文件。这就是我想要实现的目标:

enter image description here

当我不使用第二张CSV时,一切正常。当我使用另一个CSV生成表时,我必须指定数据集连接(在此之前我创建了名为DR的数据集,并且此数据集具有来自CSV的正确数据)。

enter image description here

我试过没有连接和JRCsvDatasource,但它没有工作。我还没有找到任何如何做到这一点的例子。 当我没有指定连接表是空的时。当我添加JRCsvDataSource时,它抛出异常:

  

net.sf.jasperreports.engine.JRException:   net.sf.jasperreports.engine.JRRuntimeException:   net.sf.jasperreports.engine.JRException:未知列名:FID。在   com.jaspersoft.studio.editor.preview.view.control.ReportController.fillReport(ReportController.java:596)     在   com.jaspersoft.studio.editor.preview.view.control.ReportController.access $ 20(ReportController.java:571)     在   com.jaspersoft.studio.editor.preview.view.control.ReportController $ 5.run(ReportController.java:449)     在org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

我无法在Jaspersoft Studio中为JRCsvDataSource设置列名,我认为这可能是不能使用此解决方案的真正原因。

所以问题在于Table和Dataset之间的连接。如何指定表和新数据集之间的连接?

1 个答案:

答案 0 :(得分:1)

本质上:

  • 您需要两个数据适配器,每个CSV文件一个(我称之为CSV_DA_1和CSV_DA_2)。
  • 将第一个适配器链接到报告的主数据集。
  • 将第二个导出到文件(CSV_DA_2.xml)并将表数据集的默认数据适配器指向它。 enter image description here 只有这样,Studio才会选择两个数据适配器。
  • 不确定原因,但我需要在表格数据集中手动将queryString的语言设置为csv

最终结果(.jrxml)可能如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.0.final using JasperReports Library version 6.4.1  -->
<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="Report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="6fe5ff80-463c-48a5-870b-b9eb72c8c9f3">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="CSV_DA_1"/>
    <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>
    </style>
    <style name="Table 1_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 1_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 1_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>
    </style>
    <subDataset name="TableDataset" uuid="e4a79faf-d2c6-42ff-9afd-1b73b938e128">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="different_csv/CSV_DA_2.xml"/>
        <property name="net.sf.jasperreports.data.adapter" value="CSV_DA_2.xml"/>
        <queryString language="csv">
            <![CDATA[]]>
        </queryString>
        <field name="fruitStand" class="java.lang.String"/>
        <field name="fruitId" class="java.lang.String"/>
        <field name="fruitSold" class="java.lang.Integer"/>
    </subDataset>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="number" class="java.lang.Integer"/>
    <field name="name" 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="30" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="278" height="30" uuid="675dfea9-c792-4aa2-b5cc-1506b6de4cf6"/>
                <text><![CDATA[number]]></text>
            </staticText>
            <staticText>
                <reportElement x="278" y="0" width="277" height="30" uuid="77fcc5b3-4d31-47bc-a682-3059757ff1fb"/>
                <text><![CDATA[name]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="100" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="278" height="30" uuid="aa065144-9ec4-4658-a0c5-150cdea07ae1"/>
                <textFieldExpression><![CDATA[$F{number}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="278" y="0" width="277" height="30" uuid="5284bbc5-f1e9-47f2-9b87-a0a7f7e4011c"/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <componentElement>
                <reportElement x="150" y="40" width="200" height="60" uuid="5e2faee2-cc09-469b-a5eb-917ddfdccbdf">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    <property name="com.jaspersoft.studio.table.style.table_header" value="Table 1_TH"/>
                    <property name="com.jaspersoft.studio.table.style.column_header" value="Table 1_CH"/>
                    <property name="com.jaspersoft.studio.table.style.detail" value="Table 1_TD"/>
                </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="TableDataset" uuid="886ce5d8-f55a-4573-8d7e-c47f04fa818f"/>
                    <jr:column width="66" uuid="f118bba1-a9b0-4b6e-91e5-b532f5ab3f86">
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="66" height="30" uuid="c89e45e3-1513-44fd-932f-7dcb457fdf88"/>
                                <text><![CDATA[fruitStand]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="66" height="30" uuid="b1211b0b-5321-42fd-bc44-3d13a291e1e1"/>
                                <textFieldExpression><![CDATA[$F{fruitStand}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="66" uuid="d2ad8e66-e7c8-44be-ae3d-d806bca7e9fa">
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="66" height="30" uuid="d0d7dbbb-2b2c-42f2-ad18-85bbbc1e8cc7"/>
                                <text><![CDATA[fruitId]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="66" height="30" uuid="b1df8795-8505-4533-8dad-d74c339bcc74"/>
                                <textFieldExpression><![CDATA[$F{fruitId}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="66" uuid="26903850-beb5-440c-bb69-4d4dc3d0f5dc">
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="66" height="30" uuid="dac2afb9-0f76-4f94-b98f-0336fd2e3a05"/>
                                <text><![CDATA[fruitSold]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="66" height="30" uuid="2f1f473e-04e2-4e96-a9de-a87e901c2c35"/>
                                <textFieldExpression><![CDATA[$F{fruitSold}]]></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>

输出: enter image description here