generate excel in java and download it from jsp

-1

What I'm grabbing is generating an Excel file in java from a query in sql the file is generated by clicking on a generate report button that I made in jsp and the idea is that when the Excel is generated I get the option of downloading that file from the jsp through the servlet, the truth is that I do not have much knowledge about this if you can explain how to do it thank you very much, for the moment what I'm grabbing is generating the Excel in a folder in the E: but what I need is that it appears the option to download it from the jsp I attach the part where I generate Excel

  try {

            con = new Conexion();
            query = "SELECT  Nro_Documento,Nombres,Horas_registradas_en_jira,Proyecto,Horas_laboradas_sin_extras_y_sin_novedades FROM tbljira ORDER BY Proyecto";
            st = con.getConn().prepareStatement(query);

            res = st.executeQuery();

            while (res.next()) {
                TablaJira j = new TablaJira();
                j.setNro_Documento(res.getString("Nro_Documento"));
                j.setNombres(res.getString("Nombres"));
                j.setHoras_registradas_en_jira(res.getDouble("Horas_registradas_en_jira"));
                j.setProyecto(res.getString("proyecto"));
                j.setHoras_laboradas_sin_extras_sin_novedades(res.getDouble("Horas_laboradas_sin_extras_y_sin_novedades"));
                Jira.add(j);

            }

            for (int j = 0; j < Jira.size(); j++) {

                String ide = Jira.get(j).getNro_Documento();
                String name = Jira.get(j).getNombres();
                Double h = Jira.get(j).getHoras_registradas_en_jira();
                String proyec = Jira.get(j).getProyecto();
                Double time = Jira.get(j).getHoras_laboradas_sin_extras_sin_novedades();

                if (j == 0) {
                    idean = ide;
                    nombrean = name;
                    timea = h;
                    proan = proyec;
                    horat = time;

                } else if (ide.equals(idean) & proyec.equals(proan)) {

                    suma = suma + h;
                    idean = ide;
                    proan = proyec;
                    nombrean = name;

                    horat = time;
                } else {
                    det = new Detalle_Servicios();
                    if (suma == 0) {
                        total = timea / horat;
                        totalco = total * 100;
                        BigDecimal bd = new BigDecimal(totalco);
                        bd = bd.setScale(2, RoundingMode.HALF_UP);

                        det.setPorcentaje_dedicacion(bd.doubleValue());
                        det.setHoras_laboradas_mes(horat);
                        det.setNombre_empleado(nombrean);
                        det.setNro_Documento(idean);
                        det.setHoras_dedicadas_Servicio(timea);
                        det.setNombre_proyecto(proan);
                        lst.add(det);

                        nombrean = name;
                        horat = time;
                        idean = ide;
                        proan = proyec;
                        timea = 0.0;
                        suma = h;

                    } else {
                        total = suma / horat;
                        totalco = total * 100;
                        BigDecimal bd = new BigDecimal(totalco);
                        bd = bd.setScale(2, RoundingMode.HALF_UP);
                        det.setPorcentaje_dedicacion(bd.doubleValue());
                        det.setHoras_laboradas_mes(horat);
                        det.setNombre_empleado(nombrean);
                        det.setNro_Documento(idean);
                        det.setHoras_dedicadas_Servicio(suma);
                        det.setNombre_proyecto(proan);
                        lst.add(det);

                        nombrean = name;
                        horat = time;
                        idean = ide;
                        proan = proyec;

                        suma = h;

                    }
                    if (j + 1 == Jira.size()) {
                        if (ide.equals(idean) & proyec.equals(proan)) {
                            det = new Detalle_Servicios();
                            total = suma / horat;
                            totalco = total * 100;
                            BigDecimal bd = new BigDecimal(totalco);
                            bd = bd.setScale(2, RoundingMode.HALF_UP);
                            det.setNombre_proyecto(proan);
                            det.setPorcentaje_dedicacion(bd.doubleValue());
                            det.setHoras_laboradas_mes(horat);
                            det.setNombre_empleado(nombrean);
                            det.setNro_Documento(idean);
                            det.setHoras_dedicadas_Servicio(suma);

                            lst.add(det);
                        }
                    }
                }
            }
            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFSheet sheet = wb.createSheet();
            CellStyle style = wb.createCellStyle();
            DataFormat forma = wb.createDataFormat();
            Font font = wb.createFont();
            font.setFontName("Arial");
            style.setFillForegroundColor((short) 30);
            style.setBottomBorderColor((short) 8);

            style.setFillPattern(CellStyle.SOLID_FOREGROUND);
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            style.setWrapText(true);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            font.setColor(HSSFColor.WHITE.index);
            style.setFont(font);

            XSSFRow row = sheet.createRow(1);
            XSSFCell cell;

            row.createCell(1).setCellValue("Nombre Empleado");
            row.getCell(1).setCellStyle(style);

            row.createCell(2).setCellValue("Proyecto");
            row.getCell(2).setCellStyle(style);

            row.createCell(3).setCellValue("Horas Dedicadas al servicios");
            row.getCell(3).setCellStyle(style);

            row.createCell(4).setCellValue("Horas laboradas");
            row.getCell(4).setCellStyle(style);

            row.createCell(5).setCellValue("Porcentaje de dedicacion");
            row.getCell(5).setCellStyle(style);

            int i = 2;
            for (int n = 0; n < lst.size(); n++) {
                nom = lst.get(n).getNombre_empleado();

                pro = lst.get(n).getNombre_proyecto();
                sum = lst.get(n).getHoras_dedicadas_Servicio();
                to = lst.get(n).getPorcentaje_dedicacion();
                horat = lst.get(n).getHoras_laboradas_mes();
                row = sheet.createRow(i++);
                cell = row.createCell(1);
                cell.setCellValue(nom);
                cell = row.createCell(2);
                cell.setCellValue(pro);
                cell = row.createCell(3);
                cell.setCellValue(sum);
                cell = row.createCell(4);
                cell.setCellValue(horat);
                cell = row.createCell(5);
                cell.setCellValue(to);

            }

            try {
                String ex = "ensayo";

                try (FileOutputStream archivo = new FileOutputStream("E:/copia/" + ex + ".xlsx")) {
                    wb.write(archivo);
                    archivo.close();
                }

            } catch (IOException e) {
                System.out.println(e);

            }
        } catch (SQLException ex) {
            System.out.println(ex);
            Logger.getLogger(Detalle_ServiciosDao.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
    
asked by ANGEL96 09.02.2017 в 20:41
source

1 answer

0

You have 2 options for this, the form configured with an editor and the programmatic form:

  • Use JasperReports, generating your report with JasperStudio , compiling the report (.jrxml) and adding it (.jasper ) to the classpath to then export it in the format you want (PDF, XLS, etc). example
  • Use POI library to generate it in a programmatic way. several examples
  • Here an example how to stretch it from the servlet.

    Good luck!

        
    answered by 11.02.2017 в 08:17