-1

I have already created the report (report1.jrxml) copiled it (report1.jasper) and I just want to fill it with the info of this list: List<MyObject>

public class MyObject {
    private String name;
    private int phone;
    //getters & setters...
}

The data of the list has been retrieved from a database so I want to use the info of the list to fill the report.

I want to create a pdf.

Please help me.

Kaz Miller
  • 949
  • 3
  • 22
  • 40
  • [JavaBean Data Sources](http://jasperreports.sourceforge.net/sample.reference/datasource/index.html#javabeandatasources) – Alex K Aug 01 '14 at 05:13

2 Answers2

0

If you ask about passing a collection from java code to jasperreports, the best way is to use:

net.sf.jasperreports.engine.data.JRBeanCollectionDataSource

EDIT:

Full example:

HashMap<String, Object> parameters = new HashMap<>();
List<MyObject> objects = new ArrayList<>();
JRBeanCollectionDataSource jDataSource = new JRBeanCollectionDataSource(objects);

// Load report
ClassPathResource report = new ClassPathResource("/myReport.jasper");
InputStream input = report.getInputStream();

// Fill the report with data
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(input);
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, jDataSource);

// Export the report to the output
ByteArrayOutputStream output = new ByteArrayOutputStream()
JRExporter exporter = new JRPdfExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, output);
exporter.exportReport();

// Return the byte[]
return output.toByteArray();
tfranckiewicz
  • 97
  • 1
  • 5
0

Heres the relevant code:

List<MyObject> list//list contains the data used to fill report
JasperDesign design=JRXmlLoader.load(file);//file is your .jrxml file or .jasper file
JasperReport report==JasperCompileManager.compileReport(design);
JasperPrint print=JasperFillManager.fillReport(report,parameter,new JRBeanCollectionDataSource(list));

Now you can use print object to export the report to required format. Hope it helps.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34