@SuppressWarnings({"rawtypes"}) public static synchronized void gerarRelatorio( List<? extends Entidade> lista, String formato, String caminhoImagem) throws JRException, IOException { if (lista != null && !lista.isEmpty()) { DynamicReport relatorio = criaRelatorioDinamico(lista, caminhoImagem); FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(relatorio, new ClassicLayoutManager(), lista); JRExporter jrExporter = null; String contentType = null; if ("PDF".equalsIgnoreCase(formato)) { jrExporter = new JRPdfExporter(); contentType = "application/pdf"; } if ("XLS".equalsIgnoreCase(formato)) { jrExporter = new JRXlsExporter(); contentType = "application/vnd.ms-excel"; jrExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); jrExporter.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); jrExporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); } response.setContentType(contentType); response.setHeader( "Content-Disposition", "attachment; filename=" + relatorio.getReportName() + "." + formato.toLowerCase()); ServletOutputStream outputStream = response.getOutputStream(); jrExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); jrExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); jrExporter.exportReport(); outputStream.flush(); outputStream.close(); context.renderResponse(); context.responseComplete(); } }
@FXML private void eventReporteVentas(ActionEvent event) throws JRException, ParseException { Style titleStyle = new Style(); titleStyle.setFont(new Font(18, Font._FONT_VERDANA, true)); titleStyle.setHorizontalAlign(HorizontalAlign.CENTER); Style amountStyle = new Style(); amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT); Style oddRowStyle = new Style(); oddRowStyle.setBorder(Border.NO_BORDER()); // Color veryLightGrey = new Color(230,230,230); // oddRowStyle.setBackgroundColor(veryLightGrey); oddRowStyle.setTextColor(new Color(112, 112, 112)); oddRowStyle.setFont(new Font(12, Font._FONT_ARIAL, true)); oddRowStyle.setTransparency(ar.com.fdvs.dj.domain.constants.Transparency.OPAQUE); oddRowStyle.setPaddingTop(2); oddRowStyle.setPaddingBottom(2); // Style HeaderStyle = new Style(); // HeaderStyle.setFont(new Font(12,Font._FONT_ARIAL,true)); int cont = 0; int cont3 = 0; String selecCampo = ""; for (int i = 0; i < cb.length; i++) { if (cb[i].isSelected()) { cont++; } } for (int i = 0; i < group.length; i++) { if (group[i].isSelected()) { cont3++; } } int ancho = 580 / (cont + 1); ArrayList<AbstractColumn> colum = new ArrayList<>(); AbstractColumn nombres[]; int pun = 0; String fecha1 = checkInDatePicker.getValue().toString(); String fecha2 = checkInDatePicker2.getValue().toString(); SimpleDateFormat formateador = new SimpleDateFormat("yyyy-MM-dd"); Date fechaDate1 = formateador.parse(fecha1); Date fechaDate2 = formateador.parse(fecha2); System.out.println("fecha1: " + fecha1); System.out.println("Fecha2: " + fecha2); if (cont != 0) { if (fechaDate1.before(fechaDate2) || fechaDate1.equals(fechaDate2)) { nombres = new AbstractColumn[cont]; for (int i = 0; i < cb.length; i++) { if (cb[i].isSelected()) { if (i == 4 || i == 6) { nombres[pun] = ColumnBuilder.getNew() .setColumnProperty(cb[i].getId(), Integer.class.getName()) .setTitle(cb[i].getText()) .setWidth(ancho) .setPattern("$ 0.00") // defines a pattern to apply to the values swhown (uses // TextFormat) // .setStyle(amountStyle) //special style for this column (align right) .setHeaderStyle(oddRowStyle) .build(); pun++; } else { nombres[pun] = ColumnBuilder.getNew() .setColumnProperty(cb[i].getId(), String.class.getName()) .setTitle(cb[i].getText()) .setWidth(ancho) .setHeaderStyle(oddRowStyle) .build(); pun++; } if (cont3 != 0 && i == 6) { if (selecCampo.equals("")) { selecCampo = " sum(ventas.`total`) AS 'ventas.total' "; } else { selecCampo += " , sum(ventas.`total`) AS 'ventas.total' "; } } else { if (selecCampo.equals("")) { selecCampo = camposQuery[i]; } else { selecCampo += "," + camposQuery[i]; } } } } DynamicReportBuilder drb = new DynamicReportBuilder(); DynamicReport dr = null; for (int i = 0; i < pun; i++) { drb.addColumn(nombres[i]); } Calendar c = Calendar.getInstance(); String dia = Integer.toString(c.get(Calendar.DATE)) + "/"; dia += Integer.toString(c.get(Calendar.MONTH) + 1) + "/"; dia += Integer.toString(c.get(Calendar.YEAR)); drb // .addGroups(2) .addImageBanner("./img/umaiSystem.png", 70, 70, ImageBanner.ALIGN_RIGHT) .setTitle("Reporte de ventas ") .setTitleStyle(titleStyle) .setSubtitle("Fecha de creacion: " + dia) .setDetailHeight(15) .setMargins(30, 20, 30, 15) .setPrintBackgroundOnOddRows(false) .setUseFullPageWidth(true) .setColumnsPerPage(1); String grupos = ""; int cont2 = 0; for (int i = 0; i < group.length; i++) { if (group[i].isSelected()) { if (grupos.equals("")) { grupos = group[i].getId(); } else { grupos += "," + group[i].getId(); } cont2++; } } String Rango = " where ventas.fechaVenta Between '" + fecha1 + "' and '" + fecha2 + "' "; String sql; if (cont2 != 0) { sql = " SELECT " + selecCampo + " FROM\n" + " `ventas` ventas INNER JOIN `clientes` ON clientes.`idCli` = ventas.`idCli`\n" + " INNER JOIN `usuarios` usuarios ON ventas.`idUsu` = usuarios.`idUsu` " + Rango + " GROUP BY " + grupos + " ORDER BY ventas.idVenta "; } else { sql = " SELECT " + selecCampo + " FROM\n" + " `ventas` ventas INNER JOIN `clientes` ON clientes.`idCli` = ventas.`idCli`\n" + " INNER JOIN `usuarios` usuarios ON ventas.`idUsu` = usuarios.`idUsu` " + Rango + " ORDER BY ventas.idVenta "; } System.out.println("sql: " + sql); Connection conexion = null; // conexion=Helpers.db.db.conn; conexion = Helpers.db.getDbCon().conn; drb.setQuery(sql, DJConstants.QUERY_LANGUAGE_SQL); dr = drb.build(); // Finally build the report! // JasperPrint jp=DynamicJasperHelper.generateJasperPrint(dr,new // ClassicLayoutManager(),ds); JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), conexion, null); JasperViewer.viewReport(jp, false); // finally display the report report System.out.println(sql); } else { AlertBox.display("Error", "Rango de fechas no valido"); } } else { AlertBox.display("Error", "Debe seleccionar minimo 1 campo"); } }