0

I want to generate a bill using jasper reports of different items. For this, I have defined some String arrays which I want to display in the bill as a List of items.

My code for filling the jasper report is given below

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

The Sring arrays are using to populate the ArrayList which are passed to the HashMap to fill my report. The HasMap Keys are the parameter names which are declared in Invoice.jrxml file The parameter names are given to textfiles like

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

I want to print the LIST of items but I am getting the output as comma separated values in a single line.

Sample Output here and picture below.

enter image description here

Magnilex
  • 11,584
  • 9
  • 62
  • 84
Nishant123
  • 1,968
  • 2
  • 26
  • 44
  • possible duplicate of [Passing the List of primitive type objects as datasource for subreport](http://stackoverflow.com/questions/11949333/passing-the-list-of-primitive-type-objects-as-datasource-for-subreport) & [JRBeanCollectionDataSource: How to show data from the java.util.List from JavaBean?](http://stackoverflow.com/q/12209300/876298) – Alex K Dec 29 '14 at 08:48

2 Answers2

0

You can pass ArrayList to Jasper Report just the way you pass string to Jasper Report.

param.put("list",listObject);
Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
0

String.valueOf() will return a , separated string array I do believe.

if you were using contentvalues (something i'm more familiar with) the answer would be in your put lines.

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

Your prodname key will end up being x, y, z, fun stuff, how are you doing, ....

Then when you get your prodname out as a string the next time you will get your , separated string.

So the way I've done this, and I think it works with hashmaps as well as content values just extends those. You need to first make a temp hashmap array

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

When you get done you will have an ArrayList of all your prod hashmaps. Then you can go through them all as you like and print and format them out however you like.

Zeppie
  • 179
  • 6