如何将ArrayList传递给jasper报告

时间:2014-12-27 12:08:19

标签: java jasper-reports

我想使用不同项目的jasper报告生成帐单。为此,我已经定义了一些String数组,我想在账单中显示为项目列表。

我填写碧玉报告的代码如下:

System.out.println("In printing servlet");
        String [] prod = request.getParameterValues("prodn[]");
        String [] pkgdate = request.getParameterValues("pkgdate[]");
        String [] manufact = request.getParameterValues("manufact[]");
        String [] exp = request.getParameterValues("exp[]");
        String [] batch = request.getParameterValues("batch[]");
        String [] unit = request.getParameterValues("unit[]");
        String [] qty = request.getParameterValues("qty[]");
        String [] subtot = request.getParameterValues("subtot[]");
        ArrayList<String> prodname = new ArrayList<String>();
        ArrayList<String> packagedate = new ArrayList<String>();
        ArrayList<String> manufactdate = new ArrayList<String>();
        ArrayList<String> exipry = new ArrayList<String>();
        ArrayList<String> batchno = new ArrayList<String>();
        ArrayList<String> unitprice = new ArrayList<String>();
        ArrayList<String> quantity = new ArrayList<String>();
        ArrayList<String> Subtotal = new ArrayList<String>();
        Map<String, Object> param = new HashMap<String, Object>();
        try {
        JasperPrint jasperPrint =null;
        InputStream is=this.getClass().getResourceAsStream("/com/medicam/servlets/Invoice.jrxml");
        JasperReport jasperReport = JasperCompileManager.compileReport(is);
        for(int i=0;i<prod.length;i++)
        {
            prodname.add(prod[i]);
            packagedate.add(pkgdate[i]);
            manufactdate.add(manufact[i]);
            exipry.add(exp[i]);
            batchno.add(batch[i]);
            unitprice.add(unit[i]);
            quantity.add(qty[i]);
            Subtotal.add(subtot[i]);
            System.out.println("Name :"+prod[i]+" Pkg date: "+pkgdate[i]+"Man :"+manufact[i]+" Expi: "+exp[i]+" Batch: "+batch[i]+" Unit: "+unit[i]+" Quan: "+qty[i]+" Sub:"+subtot[i]);
        }   
        param.put("prodname", String.valueOf(prodname));
        param.put("pkgdate", String.valueOf(packagedate));
        param.put("manfdate", String.valueOf(manufactdate));
        param.put("expdate", String.valueOf(exipry));
        param.put("batch", String.valueOf(batchno));
        param.put("unit", String.valueOf(unitprice));
        param.put("qty", String.valueOf(quantity));
        param.put("subtot", String.valueOf(Subtotal));
        jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JREmptyDataSource());
        JasperViewer.viewReport(jasperPrint, false);
            } 
        catch (Exception e) {
                e.printStackTrace();
            } 

Sring数组用于填充传递给HashMap的ArrayList以填充我的报告。 HasMap键是在Invoice.jrxml文件中声明的参数名称 参数名称被赋予文本文件,如

<textField>
                <reportElement x="-1" y="60" width="80" height="101" uuid="06fab25e-d80f-4535-8d85-1bea04dfca84"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression><![CDATA[$P{prodname}]]></textFieldExpression>
            </textField>

我想打印项目的LIST,但是我将输出作为逗号分隔值在a中 单行。

示例输出here和下图。

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以按照将字符串传递给ArrayList的方式将Jasper Report传递给Jasper Report

param.put("list",listObject);

答案 1 :(得分:0)

String.valueOf()将返回一个我相信的分隔字符串数组。

如果您使用的是内容值(我更熟悉的内容),那么答案就会出现在您的推文行中。

Yours:  I assume this is supposed to fill para with all the values of prodname into prodname
param.put("prodname", String.valueOf(prodname));

你的产品密钥最终将成为x,y,z,有趣的东西,你好吗,....

然后,当你下次将你的prodname作为字符串输出时,你将得到你的分隔字符串。

所以我这样做的方式,我认为它适用于哈希映射以及内容值只是扩展了那些。您需要先创建一个临时散列映射数组

    ArrayList<Map <string, object>> BuildMe;   // For me these are ArrayList<ContentValues>
    Map <String, Object> addMe;   // Again for me this is just ContentValues addMe; so I hope ive got the syntax correct
    for(int i=0;i<prod.length;i++)
    {
        addMe = new Map<String,Object>;
        addMe.add(prod[I]);
        ...
        BuildMe.add(addMe);
        System.out.println("Name :"+prod[i]+" Pkg date: "+pkgdate[i]+"Man :"+manufact[i]+" Expi: "+exp[i]+" Batch: "+batch[i]+" Unit: "+unit[i]+" Quan: "+qty[i]+" Sub:"+subtot[i]);
    }  

完成后,您将获得所有prod哈希映射的ArrayList。然后你可以根据自己的喜好浏览所有内容,然后根据自己的喜好进行打印和格式化。