使用JasperReports打印arraylist内容 [英] Print an arraylist content with JasperReports

查看:144
本文介绍了使用JasperReports打印arraylist内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java中使用 JasperReports 打印ArrayList的内容



实际上我对Jasper Reporting的了解很少
我使用iReport创建Japser Reports并使用Java命令我将打印它。



现在我想使用 JasperReports 打印学生成绩。
这是我的样本结果表。

  Reg.No SubjectCode等级学期成绩标记
132108 CMIS 1113 1 1 A 77
132107 CMIS 1213 1 2 C 57
122101 IMGT 2112 2 1 A 87
122110 IMGT 2213 2 2 A 83
112123 STAT 3113 3 1 C 55
112135 MATH 3213 3 2 B 67
132145 CMIS 1113 1 1 D 17
122118 ELTN 2213 1 2 A 90
112100 CMIS 3213 3 2 A 89
112117 SATA 3113 1 1 A 87
122104 CMIS 2213 2 2 C 54
132104 CMIS 1213 1 2 A 84

所以根据我的上表我需要打印特定学期特定学生的成绩。

在我的界面中,我有机会选择Level和the Semester。

然后相关查询自动生成,结果将从数据库中获取并使用Results对象(我创建类称为'结果'来存储特定学生的结果)我将结果数据添加到ArrayList。

现在我的ArrayList有Results对象,其中包含特定级别和特定学期的每个学生的结果。现在我想使用Jasper报告打印这个ArrayList。所以期望Jasper报告是这样的。
对于1级和第1学期,

  Reg.No CMIS1113 
132108 A
132145 D


对于2级和第2学期,

  Reg.No IMGT2213 CMIS2213 
122110 A -
122104 - C


现在我有几个问题。


1)通常我之前做的是创建Jasper T的时候使用iReport进行emplate我在创建它之前给出了相关的查询。但是这里的查询将根据所选的Level和Semester进行更改。因此结果将根据查询进行更改。由于主题正在发生变化,列标题也会发生变化。(主题数量)在每个学期和每个级别都不相似)所以我的问题是如何创建一个常见的Jasper模板来处理每种情况?(如果可能的话)



2 )我如何使用Java代码打印ArrayList。我不知道使用Jasper Report打印ArrayList内容的java代码。



我知道很少的东西关于Jasper Reports.所以,如果你能帮助我。谢谢。

解决方案

你可以传递ArrayList作为参数报告。
然后在Report中创建一个具有相同参数名称和类型(ArrayList)的子数据集。
然后创建一个表组件。使用ConnectionExpression:

  new net.sf.jasperreports.engine.JREmptyDataSource($ P {IN_ARRAY_LIST} .size())

然后将报表参数映射到表参数。
然后在表组件的TextFields中,您可以使用如下表达式:

  $ P {IN_ARRAY_LIST} .get ($ V {REPORT_COUNT})

确保TextFields的EvaluationTime属性设置为now。 / p>

UPD:
简短的演示报告,显示ArrayList源的工作原理:

 <?xml version =1.0encoding =UTF-8?> 
< jasperReport xmlns =http://jasperreports.sourceforge.net/jasperreportsxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http: //jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsdname =ArrayListla​​nguage =groovypageWidth =595pageHeight =842whenNoDataType =AllSectionsNoDetail columnWidth =555leftMargin =20rightMargin =20topMargin =20bottomMargin =20uuid =c67d12d3-80cd-46fb-8a79-5c4b0b7c76f9>
< property name =ireport.zoomvalue =1.0/>
< property name =ireport.xvalue =0/>
< property name =ireport.yvalue =0/>
< subDataset name =TableDatasetuuid =628eb9fb-e4f0-4f6c-b5b3-277a14fda6e2>
< parameter name =IN_ARRAY_LISTclass =java.util.ArrayListisForPrompting =false>
< defaultValueExpression><![CDATA [new java.util.ArrayList()]]>< / defaultValueExpression>
< /参数>
< variable name =ROW_INDEXclass =java.lang.IntegerresetType =None>
< variableExpression><![CDATA [$ V {REPORT_COUNT} - 1]]>< / variableExpression>
< / variable>
< / subDataset>
<! - 当您从JAVA呼叫您的报告时,您应该将您的ArrayList放入IN_ARRAY_LIST PARAM - >
< parameter name =IN_ARRAY_LISTclass =java.util.ArrayListisForPrompting =false>
< defaultValueExpression><![CDATA [new java.util.ArrayList()]]>< / defaultValueExpression>
< /参数>
< queryString>
<![CDATA []]>
< / queryString>
< title>
< band height =20splitType =Stretch>
< staticText>
< reportElement x =0y =0width =555height =20uuid =d4548efc-5711-42b3-b08a-cb138c5d55cf>
<! - 这仅用于演示 - 我使用PrintWhenExpression将> 2放入阵列列表 - >
< printWhenExpression><![CDATA [$ P {IN_ARRAY_LIST} .add(ROW_1)&& $ P {IN_ARRAY_LIST}。新增( ROW_2)]] GT;< / printWhenExpression>
< / reportElement>
< text><![CDATA [报告标题]]>< / text>
< / staticText>
< / band>
< / title>
< pageHeader>
< band height =20splitType =Stretch/>
< / pageHeader>
< pageFooter>
< band height =20splitType =Stretch/>
< / pageFooter>
< summary>
< band height =50splitType =Stretch>
< componentElement>
< reportElement key =tablex =0y =0width =555height =50uuid =042cdee2-a56a-49af-8a1b-740825446ed5/>
< jr:table xmlns:jr =http://jasperreports.sourceforge.net/jasperreports/componentsxsi:schemaLocation =http://jasperreports.sourceforge.net/jasperreports/components http:// jasperreports.sourceforge.net/xsd/components.xsd\">
< datasetRun subDataset =TableDatasetuuid =6f17f8a5-0bcc-4e2a-a900-cdb1e1261d24>
< datasetParameter name =IN_ARRAY_LIST>
< datasetParameterExpression><![CDATA [$ P {IN_ARRAY_LIST}]]>< / datasetParameterExpression>
< / datasetParameter>
< dataSourceExpression><![CDATA [new net.sf.jasperreports.engine.JREmptyDataSource($ P {IN_ARRAY_LIST} .size())]]>< / dataSourceExpression>
< / datasetRun>
< jr:column width =555uuid =236aa389-6073-48fa-9b0f-02dcec80551e>
< jr:columnHeader height =30rowSpan =1>
< staticText>
< reportElement x =0y =0width =555height =30uuid =18d3f3cc-13fa-4203-b239-45739b1839dc/>
< text><![CDATA [ArrayList Field Value]]>< / text>
< / staticText>
< / jr:columnHeader>
< jr:detailCell height =20rowSpan =1>
< textField>
< reportElement x =0y =0width =555height =20uuid =a5bd535e-091b-4eef-a0af-e70fc7d2cce2/>
< textFieldExpression><![CDATA [$ P {IN_ARRAY_LIST} .get($ V {ROW_INDEX})]]>< / textFieldExpression>
< / textField>
< / jr:detailCell>
< / jr:column>
< / jr:table>
< / componentElement>
< / band>
< / summary>
< / jasperReport>

我希望现在很清楚。


I want to print the content of an ArrayList using JasperReports in Java

Actually I am know little about Jasper Reporting I use iReport to create Japser Reports and using Java command I will print it.

Now I want to print the student results using JasperReports. This is my sample results table.

Reg.No  SubjectCode  Level  Semester  Grade  Marks
132108  CMIS 1113    1        1        A      77
132107  CMIS 1213    1        2        C      57
122101  IMGT 2112    2        1        A      87
122110  IMGT 2213    2        2        A      83
112123  STAT 3113    3        1        C      55
112135  MATH 3213    3        2        B      67
132145  CMIS 1113    1        1        D      17
122118  ELTN 2213    1        2        A      90
112100  CMIS 3213    3        2        A      89
112117  SATA 3113    1        1        A      87
122104  CMIS 2213    2        2        C      54
132104  CMIS 1213    1        2        A      84

So according to my above table I need to print "Results of student in particular level in particular semester".
In my interface I have facility to select the Level and the Semester.
Then the relevant query with generate automatically and results will obtain from the database and using Results object(I create class called 'Results' to store the results of a particular student) I add the results data to a ArrayList.
Now my ArrayList have the Results objects which carries the results of each students in particular level and particular semester.Now I want to print this ArrayList using Jasper report.So expected Jasper Report is like this.
For Level 1 and Semester 1,

Reg.No  CMIS1113   
132108    A
132145    D


For Level 2 and Semester 2,

Reg.No  IMGT2213  CMIS2213   
122110    A          -
122104    -          C


Now I have several problems.

1) Normally what I did previously is when I create Jasper Template using iReport I gave the relevant query before creating it.But here query will be changed according to selected Level and Semester.So the result will be changed according to the query.And column headers also changed because subjects are changing.(Number of subject are not similar in each semester and each level)So my question is how can I create a common Jasper Templates to Handle each situation?(if possible)

2) How I print the ArrayList using Java code.I do not know the java code to print the content of ArrayList using Jasper Report.

I know very few things about Jasper Reports.So if you can please help me.Thank You.

解决方案

you can pass the ArrayList to report as a parameter. Then in Report create a Subdataset with the same parameter name and type (ArrayList). Then Create a table component. use the ConnectionExpression:

new net.sf.jasperreports.engine.JREmptyDataSource($P{IN_ARRAY_LIST}.size())

Then map report parameter to table parameter. Then in TextFields in the table component you can use the expression like that:

$P{IN_ARRAY_LIST}.get($V{REPORT_COUNT})

Make sure that EvaluationTime property of your TextFields is set to now.

UPD: Short demo report which shows how ArrayList source works:

<?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="ArrayList" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c67d12d3-80cd-46fb-8a79-5c4b0b7c76f9">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <subDataset name="TableDataset" uuid="628eb9fb-e4f0-4f6c-b5b3-277a14fda6e2">
        <parameter name="IN_ARRAY_LIST" class="java.util.ArrayList" isForPrompting="false">
            <defaultValueExpression><![CDATA[new java.util.ArrayList()]]></defaultValueExpression>
        </parameter>
        <variable name="ROW_INDEX" class="java.lang.Integer" resetType="None">
            <variableExpression><![CDATA[$V{REPORT_COUNT} - 1]]></variableExpression>
        </variable>
    </subDataset>
    <!-- WHEN YOU CALL YOUR REPORT FROM JAVA, YOU SHOULD PUT YOUR ArrayList into IN_ARRAY_LIST PARAM-->
    <parameter name="IN_ARRAY_LIST" class="java.util.ArrayList" isForPrompting="false">
        <defaultValueExpression><![CDATA[new java.util.ArrayList()]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <title>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="555" height="20" uuid="d4548efc-5711-42b3-b08a-cb138c5d55cf">
                    <!-- THIS IS FOR DEMO ONLY - I PUT 2 ROWS INTO ARRAY LIST USING PrintWhenExpression -->
                    <printWhenExpression><![CDATA[$P{IN_ARRAY_LIST}.add("ROW_1") && $P{IN_ARRAY_LIST}.add("ROW_2")]]></printWhenExpression>
                </reportElement>
                <text><![CDATA[Report Title]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="20" splitType="Stretch"/>
    </pageHeader>
    <pageFooter>
        <band height="20" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="50" splitType="Stretch">
            <componentElement>
                <reportElement key="table" x="0" y="0" width="555" height="50" uuid="042cdee2-a56a-49af-8a1b-740825446ed5"/>
                <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="6f17f8a5-0bcc-4e2a-a900-cdb1e1261d24">
                        <datasetParameter name="IN_ARRAY_LIST">
                            <datasetParameterExpression><![CDATA[$P{IN_ARRAY_LIST}]]></datasetParameterExpression>
                        </datasetParameter>
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource($P{IN_ARRAY_LIST}.size())]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="555" uuid="236aa389-6073-48fa-9b0f-02dcec80551e">
                        <jr:columnHeader height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="555" height="30" uuid="18d3f3cc-13fa-4203-b239-45739b1839dc"/>
                                <text><![CDATA[ArrayList Field Value]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell height="20" rowSpan="1">
                            <textField>
                                <reportElement x="0" y="0" width="555" height="20" uuid="a5bd535e-091b-4eef-a0af-e70fc7d2cce2"/>
                                <textFieldExpression><![CDATA[$P{IN_ARRAY_LIST}.get($V{ROW_INDEX})]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </summary>
</jasperReport>

I hope this is clear now.

这篇关于使用JasperReports打印arraylist内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆