如何将JRBeanCollectionDataSource数据添加到Jasper Report中的内容表中?

时间:2013-05-18 18:14:13

标签: java jasper-reports

我在JRBeanCollectionDataSource中有数据。我想将这些数据添加到Jasper Report中的内容表中。我试过但无法映射结构。我已添加以下代码供您参考。

ArrayList<StudentDetails> studentReportList = new ArrayList<StudentDetails>();
studentReportList = getStudentReportList();
JasperReport jasperReport = null;
JasperPrint jasperPrint = null;
HashMap<String, Object> reportParms = new HashMap<String, Object>();
JasperDesign jasperDesign = JRXmlLoader.load("studentReport.jrxml");
jasperReport = JasperCompileManager.compileReport(jasperDesign);
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(studentReportList);
jasperPrint = JasperFillManager.fillReport(jasperReport,reportParms,beanColDataSource);

请找到studentReport.xml文件

<?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="AlarmReport" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="30" rightMargin="30" topMargin="30" bottomMargin="30" whenResourceMissingType="Empty">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="studentName" class="java.lang.String">
        <fieldDescription><![CDATA[studentName]]></fieldDescription>
    </field>
    <title>
            <band height="23">
                <staticText>
                    <reportElement x="10" y="5" width="150" height="15"/>
                    <textElement textAlignment="Left" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[Student Name]]></text>
                </staticText>
            </band>
    </title>
    <detail>
        <band height="23">
            <textField>
                <reportElement x="10" y="5" width="150" height="15"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{studentName}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

我想在内容表中显示但无法在表格元素中添加。

 <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 3" uuid="e10fc76c-07f0-4640-ae0c-fd7e66f5991e">
                                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(1)]]></dataSourceExpression>
                                </datasetRun>
                                <jr:column width="66" uuid="b19fb361-d61a-4633-a23a-3e714f93381f">
                                    <jr:tableHeader style="table 2_TH" height="17" rowSpan="1">
                                        <staticText>
                                            <reportElement uuid="ab2cac92-a59b-4278-b5d9-31ed50d98918" x="0" y="0" width="66" height="17"/>
                                            <textElement verticalAlignment="Middle">
                                                <font size="8" isBold="true"/>
                                            </textElement>
                                            <text><![CDATA[Student Name]]></text>
                                        </staticText>
                                    </jr:tableHeader>
                                    <jr:detailCell style="table 2_TD" height="16" rowSpan="1">
                                        <textField>
                                            <reportElement uuid="5736bf99-52c4-4787-a223-a3d931c13b4b" x="0" y="0" width="66" height="16"/>
                                            <textElement/>
                                            <textFieldExpression><![CDATA[$F{studentName}]]></textFieldExpression>
                                        </textField>
                                    </jr:detailCell>
                                  </jr:column>
                        </jr:table>
                    </componentElement>
                </band>
            </detail>
        </jasperReport>

1 个答案:

答案 0 :(得分:0)

可能是getStudentReportList()返回一个空列表,因此不会打印细节带。