Arquivo da tag: Excel

Geração de relatório com JasperReports em Api Rest

Basta colocar no seu método esse bloco de código.

Map params = new HashMap<>();
JasperReport jasperReport = JasperCompileManager.compileReport(getClass().getResourceAsStream(getPathReport()));
JRBeanCollectionDataSource colecaoDeDados = new JRBeanCollectionDataSource(dao.findeAll());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, colecaoDeDados);

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "inline; filename=consulta.xls");

final OutputStream outStream = response.getOutputStream();
JRXlsExporter exporter = new JRXlsExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outStream));
exporter.exportReport();

Share and Enjoy !

0Shares
0 0