问题
I'm trying to use Jasper Reports to help with reporting in my application. I will have to display my reports in HTML (JSP) and would also need to be able to export the reports to PDF from within my web page.
Most of my reports require multiple parameters, and I can't figure out how to pass them from my Servlet (if I have to pass them there) to the report.
P.S: I'm not using frameworks of any sort, it would be great if you could suggest a framework free implementation.
回答1:
//Preparing data - change this as per your requirement
List<DataDTO> dataDTOList = new ArrayList<DataDTO>();
DataDTO dataDTO = new DataDTO;
dataDTO.setFirstName("FirstName"); // in your Jasper field name is 'firstName' as type String
dataDTO.setLastName("LastName"); // in your Jasper field name is 'lastName' as type String
dataDTOList.add(dataDTO);
// Adding data
JRDataSource jrdatasource = new JRBeanCollectionDataSource(dataDTOList);
// Exporting report
File jasperFile = new File("C:/YourReport.jasper"); // change this
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new FileInputStream(jasperFile.getAbsolutePath()));
Map parameters = new HashMap();
JRConcurrentSwapFile jrSwapFile = new JRConcurrentSwapFile("C:/PDFOutput/"),30,2);
JRSwapFileVirtualizer virtualizer = new JRSwapFileVirtualizer(2,jrSwapFile,true);
parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);
JasperPrint jpPrintObj = JasperFillManager.fillReport(jasperReport,parameters,jrdatasource);
JasperExportManager.exportReportToPdfFile(jpPrintObj,"C:/PDFOutput/");`
回答2:
you can use HashMap like
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("parameterName","value");
JasperPrint print = JasperFillManager.fillReport(report,map,con);
回答3:
This example will help you, it doesn't require any framework. It exports the report as a PDF. And you can use map as Anil had already explained
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("parameterName","value");
map.put("parameterName2","value2");
map.put("parameterName3","value3");
JasperPrint print = JasperFillManager.fillReport(report,map,con);
to pass multiple parameters.
来源:https://stackoverflow.com/questions/8709199/how-to-pass-multiple-parameters-to-reports-and-export-to-pdf-in-jasperreports-fr