在iReports和Jasper以及java bean数据源中使用Table功能

时间:2012-09-23 19:41:31

标签: java jasper-reports ireport

我花了12小时的大部分时间试图弄清楚如何让图表和表格在碧玉中工作。这篇文章是关于表格(对于我的图表问题,可能是同样的问题,see here

我想使用iReports面板中的Table工具创建一个包含两列的表。我有一个列表(以前是一个 ArrayList ,但我现在正在尝试一个List,因为我看过的其中一个教程使用了它而没有问题)在 ProtoReport 数据对象列表中的字符串 ProtoReport List 是我在 JRBeanCollectionDataSource 中打包并传递给JasperFill的那个。 我希望 tableData List 填充我的表中的一列。当我根据我对Jasper的有限经验使我想想我应该如何设置时,我得到以下错误:

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
            value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.util.List)field_tableData.getValue())); //$JR_EXPR_ID=12$
                        <---------------------------------------------------->
2. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
            value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.util.List)field_tableData.getOldValue())); //$JR_EXPR_ID=12$
                        <---------------------------------------------------->
3. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
            value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.util.List)field_tableData.getValue())); //$JR_EXPR_ID=12$
                        <---------------------------------------------------->
3 errors

at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:204)
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:240)
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:173)
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:448)
at org.reportprotojava.protosheet.Program.main(Program.java:134)

这是我的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="ReportPrototype.jrxml" pageWidth="595" pageHeight="842" columnWidth="495" leftMargin="57" rightMargin="43" topMargin="43" bottomMargin="43" uuid="10825c57-f953-4166-bf03-8ecabe8a8f47">
<property name="ireport.zoom" value="0.75"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="128"/>
<style name="table">
    <box>
        <pen lineWidth="1.0" lineColor="#000000"/>
    </box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
    <box>
        <topPen lineWidth="0.5" lineColor="#000000"/>
        <bottomPen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
    <box>
        <topPen lineWidth="0.5" lineColor="#000000"/>
        <bottomPen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
    <box>
        <topPen lineWidth="0.5" lineColor="#000000"/>
        <bottomPen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table 1">
    <box>
        <pen lineWidth="1.0" lineColor="#000000"/>
    </box>
</style>
<style name="table 1_TH" mode="Opaque" backcolor="#F0F8FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table 1_CH" mode="Opaque" backcolor="#BFE1FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
    <conditionalStyle>
        <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
        <style backcolor="#EFF7FF"/>
    </conditionalStyle>
</style>
<style name="table 2">
    <box>
        <pen lineWidth="1.0" lineColor="#000000"/>
    </box>
</style>
<style name="table 2_TH" mode="Opaque" backcolor="#F0F8FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table 2_CH" mode="Opaque" backcolor="#BFE1FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table 2_TD" mode="Opaque" backcolor="#FFFFFF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
    <conditionalStyle>
        <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
        <style backcolor="#EFF7FF"/>
    </conditionalStyle>
</style>
<subDataset name="ChartData" uuid="fc9ec0af-3e1a-40a7-8eb4-9ad30a266dee"/>
<subDataset name="Table Dataset 1" uuid="70531f85-19bf-4bd7-b801-6ed08b189b34">
    <field name="tableData" class="java.util.List"/>
</subDataset>
<queryString language="SQL">
    <![CDATA[]]>
</queryString>
<field name="title" class="java.lang.String"/>
<field name="logoLocation" class="java.lang.String"/>
<field name="picLocation" class="java.lang.String"/>
<field name="paragraphText" class="java.lang.String"/>
<field name="tableData" class="java.util.List"/>
<detail>
    <band height="756" splitType="Stretch">
        <textField isStretchWithOverflow="true" pattern="">
            <reportElement uuid="519c6bb5-72f9-4c25-8e91-47865ae0c9df" mode="Opaque" x="38" y="42" width="378" height="45" forecolor="#000099"/>
            <textElement textAlignment="Center" verticalAlignment="Middle" markup="html">
                <font size="26"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
        </textField>
        <image onErrorType="Icon">
            <reportElement uuid="3759a707-32a4-49ef-a9c6-b0ad7136f738" x="216" y="273" width="279" height="246"/>
            <imageExpression><![CDATA[$F{picLocation}]]></imageExpression>
        </image>
        <image onErrorType="Icon">
            <reportElement uuid="f989f871-32ea-4f13-ae3f-3f487cde76dd" x="295" y="0" width="200" height="42"/>
            <imageExpression><![CDATA[$F{logoLocation}]]></imageExpression>
        </image>
        <xyLineChart>
            <chart>
                <reportElement uuid="ae87fc13-b92e-4a2a-b218-d395343f6028" x="0" y="537" width="495" height="203"/>
                <chartTitle/>
                <chartSubtitle/>
                <chartLegend/>
            </chart>
            <xyDataset>
                <dataset>
                    <datasetRun subDataset="ChartData" uuid="de7fb84d-17ea-4e5e-82bf-2015e72e4982"/>
                </dataset>
            </xyDataset>
            <linePlot>
                <plot/>
            </linePlot>
        </xyLineChart>
        <textField>
            <reportElement uuid="565b981c-ca6f-4eab-ab3e-683b1e2b4d03" stretchType="RelativeToTallestObject" mode="Opaque" x="0" y="103" width="495" height="144" backcolor="#CCCCCC"/>
            <textElement markup="html">
                <font size="6"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{paragraphText}]]></textFieldExpression>
        </textField>
        <componentElement>
            <reportElement uuid="8eb2d942-e4be-4b86-b409-9e1b91f6b4c4" key="table 2" style="table 2" x="13" y="273" width="180" height="246"/>
            <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 1" uuid="40906b08-698d-4979-ae31-7f1689859954">
                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JRBeanCollectionDataSource($F{tableData})]]></dataSourceExpression>
                </datasetRun>
                <jr:column uuid="f1282add-60a7-49d7-b66d-c8bd912dc161" width="90">
                    <jr:tableHeader style="table 2_TH" height="30"/>
                    <jr:detailCell style="table 2_TD" height="20">
                        <textField>
                            <reportElement uuid="5c95b1c4-bfb0-4364-83da-28d78e2d0555" x="0" y="0" width="90" height="20"/>
                            <textElement/>
                            <textFieldExpression><![CDATA[""+$F{tableData}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column uuid="ec2f8c0c-e950-4b74-9d1e-851f68deedf3" width="90">
                    <jr:tableHeader style="table 2_TH" height="30"/>
                    <jr:detailCell style="table 2_TD" height="20"/>
                </jr:column>
            </jr:table>
        </componentElement>
    </band>
</detail>

这是我的ProtoReport类的初学者(getter,setter等切断):

public class ProtoReport {


private String outputFileName;
private String title;
private String logoLocation;
private String paragraphText;
private List<String> tableData;
private String picLocation;
private int[][] graphData;  //TODO decide how to store chart data
private ChartData chartData;
private String path;




//default constructor
public ProtoReport() {

    // Initialize object fields
    outputFileName = "PrototypeReport";
    title = "Prototype Report";
    paragraphText = "Default text";

    tableData = null;
    chartData = new ChartData();

    //set path to working directory
    path = System.getProperty("user.dir");

    //default to assumed report location 
    //(ie same folder as .jrxml and .jasper files)
    logoLocation = path + "\\reports\\logo.jpg";
    picLocation = path + "\\reports\\pic.jpg";

}

//constructor
public ProtoReport(String title, String logoLocation, 
                        String picLocation, ChartData chartData) {

        // Initialize object fields
        outputFileName = "PrototypeReport";
        this.title = title;
        paragraphText = "Default text";

        //tableData = new ArrayList<String>();
        tableData = null;
        this.chartData = chartData;

        //set path to working directory
        path = System.getProperty("user.dir");

        //picture locations
        this.logoLocation = logoLocation;
        this.picLocation = picLocation;

    }



//Getters and Setters

//Self referencing method for use in iReport field description
    public ProtoReport getMe()
     {
      return this;
     }

最后,我的主要相关部分:

public static void main(String[] args) {

    List<ProtoReport> listOfReports = new ArrayList<ProtoReport>();

    ProtoReport protoReport1 = new ProtoReport();
    ProtoReport protoReport2 = new ProtoReport();

//test table.. since nothing else will work...
    List<String> testTable = new ArrayList<String>();
    testTable.add("First entry test");
    testTable.add("Second entry test");
    protoReport1.setTableData(testTable);

    protoReport1.getTableData().add("First entry");
    protoReport1.getTableData().add("Second entry");
 listOfReports.add(protoReport1);

//and wrap the ArrayList in a JRBeanCollectionDataSource
    JRBeanCollectionDataSource beanBurritoWrap = new JRBeanCollectionDataSource(listOfReports);

    //build the jasper report
    JasperReport jasperReport;
    JasperPrint jasperPrint;
    HashMap<String, Object> hashMap = new HashMap<>();
    boolean reportCreated;

    try {

        jasperReport = JasperCompileManager.compileReport(jrxmlLocation);
        jasperPrint = JasperFillManager.fillReport(jasperReport, hashMap, beanBurritoWrap);
        JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
        reportCreated=true;
    }
    catch (JRException e) {
          e.printStackTrace();
          reportCreated=false;
    }

由于这与我的图表中的问题类似,我怀疑它们有相同的原因。我一直在阅读有关如何使用这些功能的所有内容,但有些事情只是没有点击......如何在iReport中设置连接以用tableData中的字符串填充我的表?我是否需要每列的字符串列表,或者是否有更智能的方法来完成所有这些(请记住我想用POJO完成所有操作,即没有sql数据库)。

1 个答案:

答案 0 :(得分:2)

您在jrxml中提到的包名称是错误的。它是net.sf.jasperreports.engine.data.JRBeanCollectionDataSource