| package com.test; import java.util.List; public class MainVO { private String title; private String time; private List<SubVO> subList; } |
| package com.test; public class SubVO { private String name; |
| package com.test; public class TestSubReport extends JRAbstractBeanDataSourceProvider { public TestSubReport() { super(MainVO.class); } public JRDataSource create(JasperReport arg0) throws JRException { /** *测试数据,在使用中,不需要继承JRAbstractBeanDataSourceProvider, *只需要把集合类封装到JRBeanCollectionDataSource中就可以了 **/ List<MainVO> mainList = new ArrayList<MainVO>(); List<SubVO> list = new ArrayList<SubVO>(); MainVO vo; /**测试数据自写*/ …… return new JRBeanCollectionDataSource(mainList); } |
| public static void main(String[] args) { String filename = "bin/SubReport.jasper"; String outFileName = "bin/Out.html"; /**测试数据mainList自写*/ try { JasperPrint print = JasperFillManager.fillReport(filename, new HashMap(),new JRBeanCollectionDataSource(mainList)); JRExporter exporter = new JRHtmlExporter(); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName); exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); exporter.exportReport(); } catch (JRException e) { e.printStackTrace(); } } |
| package com.test; public class reportScriptlet extends JRAbstractScriptlet { /**其余方法省略*/ @Override public void afterDetailEval() throws JRScriptletException { System.out.println("afterDetailEval..."); List subList = (List)getFieldValue("subList"); JRDataSource jr = new JRBeanCollectionDataSource(subList); /** *该值是在父报表中定义的一个变量Variables, *类型为net.sf.jasperreports.engine.JRDataSource *(需要手动填写Class Type) **/ setVariableValue("other", jr); } } |


| package com.test; public class CRDSFactory{ public static JRDataSource createDatasource(){ List<SubVO> list = new ArrayList<SubVO> (); /**测试数据自写*/ …… return new JRBeanCollectionDataSource(list); } } |




