Passing the List of primitive type objects as datasource for subreport

前端 未结 3 1199
面向向阳花
面向向阳花 2020-11-30 05:18

I need to pass to my subreport a dataSource with help of master report\'s List parameter. I don\'t know what is

3条回答
  •  情书的邮戳
    2020-11-30 05:33

    You can use this datasource expression for passing java.util.List (via parameter) to subreport:

    
    

    The working sample, master report:

    
    
        
        
            "]]>
        
        
            
        
        
        
        
        
            
                
                    
                    
                        
                        
                        
                        
                    
                    
                        
                        
                            
                        
                        
                        
                    
                    
                        
                        
                        
                            
                        
                        
                    
                    
                        
                        
                        
                            
                        
                        
                    
                    
                        
                        
                        
                            
                        
                        
                    
                
            
        
    
    

    The subreport:

    
    
        
        
        
        
        
        
            <band height="39">
                <textField>
                    <reportElement x="220" y="14" width="161" height="20"/>
                    <box leftPadding="10"/>
                    <textElement>
                        <font isBold="true" isItalic="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA["City param: " + $P{cityParam}]]></textFieldExpression>
                </textField>
            </band>
        
        
            
                
                    
                    
                    
                    
                
                
                    
                    
                    
                    
                
            
        
        
            
                
                    
                    
                    
                        
                    
                    
                
            
        
    
    

    The Java code for passing List:

    Map params = new HashMap();
    
    List beansList = new ArrayList();
    
    // The TestBean class constructor is: 
    //public TestBean(String city, Integer id, String station)
    TestBean bean = new TestBean("Dallas", 10, "Central park st.");
    beansList.add(bean);
    
    bean = new TestBean("Dallas", 11, "Railway st.");
    beansList.add(bean);
    
    bean = new TestBean("Dallas", 12, "Market st.");
    beansList.add(bean);
    
    bean = new TestBean("Lyon", 20, "Airport st.");
    beansList.add(bean);
    
    params.put("listParam", beansList);
    
    JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDemoHsqldbConnection());
    
    JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
    

    The result will be (view of generated PDF file):

    Generated result in PDF format


    You can look at the implementations of net.sf.jasperreports.engine.JRDataSource. The most appropriate for your case are: JRBeanCollectionDataSource and JRBeanArrayDataSource. As you can see they are both Bean-based.

    I think you can easily convert your List to the List.

    Or you can implement your own JRDataSource.

提交回复
热议问题