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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12555651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:21:14  来源:igfitidea点击:

Using the Table feature in iReports and Jasper and java bean data sources

javajasper-reportsireport

提问by eljaydub

I've spend the better part of 12hours now trying to figure out how to get charts and tables to work in jasper. This post is about the tables (for my chart issues, which are likely the same problem, see here)

我现在花了 12 个小时的大部分时间试图弄清楚如何让图表和表格在 jasper 中工作。这篇文章是关于表格的(对于我的图表问题,可能是同样的问题,请参见此处

I want to make a table with two columns using the Table tool in the iReports palette. I have a List(previously was an ArrayListbut I'm trying a List now since one of the tutorials I watched used that instead and they didn't have a problem) of Stringinside of the list of ProtoReportdata object. The ProtoReport Listis the one I wrap in a JRBeanCollectionDataSourceand pass to JasperFill. I want the tableData Listto fill one of the columns of my table. When I set this up according to how my limited experience with Jasper makes me thinkI should, I get the following error:

我想使用 iReports 面板中的表格工具制作一个包含两列的表格。我在ProtoReport数据对象的列表中有一个StringList(以前是一个ArrayList但我现在正在尝试一个 List,因为我观看的其中一个教程使用了它并且他们没有问题)。该ProtoReport名单是在一个我包JRBeanCollectionDataSource,并传递给JasperFill。我希望tableData 列表填充我的表的列之一。当我根据我对 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)

Here is my jrxml:

这是我的 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>

And here is the beginner of my ProtoReport class (getters, setters, etc cut off):

这是我的 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;
     }

And finally, the relevant parts of my Main:

最后,我的 Main 的相关部分:

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;
    }

Since this is a similar problem to the one I'm having with my chart I suspect they have the same cause. I've been reading everything I can about how to use these features but somethings just not clicking... How to I set up the connections in iReport to fill my table with the strings in tableData? Do I need a list of strings for each column or is there a smarter way to do all this (keeping in mind I want to do it all with POJO's, ie with no sql database).

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

采纳答案by Balaji Paulrajan

The package name you mentioned in the jrxml is wrong. It is net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.

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