public static void visualizaJasperEmExcel(String nomeArquivoJasper, List<?> listaDados, Map<String, Object> parametros) { FacesContext facesContext = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); try { ServletOutputStream streamDeSaida = response.getOutputStream(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachments; filename=\"relatorio.xls\""); JRDataSource dados = new JRBeanCollectionDataSource(listaDados); JasperPrint impressoraJasper = JasperFillManager.fillReport(nomeArquivoJasper, parametros, dados); // configurar o exporter JRXlsExporter exporter = new JRXlsExporter(); // exporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, impressoraJasper); // exporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, streamDeSaida); // exporter.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.TRUE); // exporter.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration(); configuration.setWhitePageBackground(true); configuration.setOnePagePerSheet(true); exporter.setConfiguration(configuration); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(streamDeSaida)); exporter.exportReport(); facesContext.responseComplete(); } catch (JRException | IOException e) { LogUtils.generate(e); } }
@Override public StreamedContent processReport() throws Exception { beforeProcess(); JasperPrint jasperPrint = processaDadosJasper(); if (jasperPrint == null) { JavaScriptUtils.showMessageHeaderWarning(BundleUtils.messageBundle("message.naoExisteDadosImpressao")); return null; } if (typeReport() == FileType.PDF) { File arquivoPdfGeracao = File.createTempFile(fileName(), ".pdf"); InputStream pdfStream = new FileInputStream(arquivoPdfGeracao.getAbsolutePath()); JasperExportManager.exportReportToPdfFile(jasperPrint, arquivoPdfGeracao.getAbsolutePath()); return new DefaultStreamedContent(pdfStream, "application/pdf", fileName() + ".pdf"); } else if (typeReport() == FileType.EXCEL) { File arquivoPdfGeracao = File.createTempFile(fileName(), ".xlsx"); OutputStream outputStream = new FileOutputStream(arquivoPdfGeracao.getAbsolutePath()); JRXlsExporter exporter = new JRXlsExporter(); exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream)); SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration(); configuration.setOnePagePerSheet(true); configuration.setDetectCellType(true); configuration.setCollapseRowSpan(false); exporter.setConfiguration(configuration); exporter.exportReport(); InputStream pdfStream = new FileInputStream(arquivoPdfGeracao.getAbsolutePath()); return new DefaultStreamedContent(pdfStream, FileType.EXCEL.getContentType(), fileName() + ".xlsx"); } return null; }
public ReportExportConfiguration getReportConfiguration() { reportConfiguration.setIgnoreGraphics(Boolean.FALSE); reportConfiguration.setCollapseRowSpan(Boolean.FALSE); reportConfiguration.setDetectCellType(Boolean.TRUE); reportConfiguration.setFontSizeFixEnabled(Boolean.TRUE); reportConfiguration.setRemoveEmptySpaceBetweenColumns(Boolean.FALSE); reportConfiguration.setRemoveEmptySpaceBetweenRows(Boolean.FALSE); reportConfiguration.setIgnoreCellBorder(Boolean.FALSE); reportConfiguration.setImageBorderFixEnabled(Boolean.FALSE); reportConfiguration.setShowGridLines(Boolean.FALSE); reportConfiguration.setIgnoreCellBackground(Boolean.FALSE); reportConfiguration.setWrapText(Boolean.FALSE); reportConfiguration.setOnePagePerSheet(Boolean.TRUE); reportConfiguration.setColumnWidthRatio(1.25f); return reportConfiguration; }