-1

I have a Java Web App that i'm using. It's more of a business process web app.

I want to add a feature to it for generating reports. I want to try Jasper Reports. So I researched on about how to connect JavaWebApp to jasper report, there's results but there is nothing about the ArrayList/list.

I want the datasource as ArrayList/List because i'm using MVC framework on this project. So far all the posts, videos I found are nothing similar as they are connecting the JasperReport to a Database.

Can someone enlighten me on how to call a jasper report on a java web app and pass arrayList/List as its data source. Thanks in advance.

HjReyes
  • 41
  • 6
  • My concern is not as same as his. That example is referencing a static class in iReport. It generate the report in the iReport itself. It does not have a main class. In my case I have a program that I need to generate the report from. I have a main class here. It will call the iReport and pass a datasource. My question is how can I use that datasource to show the datas. – HjReyes Feb 24 '18 at 15:41

1 Answers1

0

It takes a few steps. Try the following:

List<Object> dataBeans = ...//get your beans.
JRBeanCollectionDataSource beansDataSource = 
    new JRBeanCollectionDataSource(dataBeans);
Map<String, Object> parameters = ...//create your parameter map to fill parameters in your template.
JasperPrint jasperPrint = 
     JasperFillManager.fillReport(templateStream, parameters, beansDataSource);

//Export the report:
//PDF:
byte[] pdfBinary = JasperExportManager.exportReportToPdf(jasperPrint);

//HTML:
String tempFile = "/tmp/report.html";

File file = new File(tempFile);
file.createNewFile();

JasperExportManager.exportReportToHtmlFile(jasperPrint, tempFile);
//Then you can read it back into a byte stream if needed, but that 
//takes standard IO...
ernest_k
  • 44,416
  • 5
  • 53
  • 99