create excel in java by consulting in MySQL

0

I'm trying to create an excel file using a mysql query, what happens is the following I do the sql query with the data I need to make a sums and divides then I add all those results to an arraylist what is not how to do to get each of these data to enter it in the corresponding cells

try {
 List listaFinal = new ArrayList();
 List listaCabeceras = new ArrayList();
 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);

 }
 List fila = null;
 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;

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

   suma = suma + h + timea;
   idean = ide;

   nombrean = name;
   timea = 0.0;
   horat = time;
  } else {

   idean = ide;
   total = suma / time;
   det.setTiempo_dedicado(total);
   det.setHoras_laboradas(horat);
   det.setNombre_empleado(nombrean);
   det.setNro_Documento(idean);
   det.setHoras_dedicadas(suma);
   det.setNombre_proyecto(proan);


   idean = ide;
   proan = proyec;
   timea = h;
   suma = 0.0;
   lst.add(det);
  }
  if (j + 1 == Jira.size()) {
   if (ide.equals(idean) & proyec.equals(proan)) {

    //suma = suma + h + timea;
    // idean = ide;
    // timea = 0.0;
    total = suma / time;
    det.setNombre_proyecto(proan);
    det.setTiempo_dedicado(total);
    det.setHoras_laboradas(horat);
    det.setNombre_empleado(nombrean);
    det.setNro_Documento(idean);
    det.setHoras_dedicadas(suma);
    lst.add(det);

   }
  }
 }

 //acá es donde estoy tratando de sacar los elementos del ArrayList para agregarlos a las celdas correspondientes y no he podido, si alguien me puede ayudar muchas gracias u otra forma mejor de hacero
 for (int n = 0; n < lst.size(); n++) {

  row = sheet.createRow(n);
  cell = row.createCell(1);
  cell.setCellValue("");
  cell = row.createCell(2);
  cell.setCellValue("");
  cell = row.createCell(3);
  cell.setCellValue("");
  cell = row.createCell(4);
  cell.setCellValue("");
  cell = row.createCell(5);
  cell.setCellValue("");
  cell = row.createCell(6);
  cell.setCellValue("");

 }

 cell.setCellValue(res.getString("Horas_registradas_en_jira"));
 cell.setCellValue(res.getDouble("Horas_laboradas_sin_extras_y_sin_novedades"));

 try {
  String ex = "ensayo";

  try (FileOutputStream archivo = new FileOutputStream("E:/copia/" + ex + ".xlsx")) {
   wb.write(archivo);
   archivo.close();
  }
    
asked by victormbrt 08.02.2017 в 17:09
source

1 answer

1

I think your problem at this point is that you are not going through the rows properly to put together a table in Excel.

  

NOTE: Keep in mind that positions in Excel do not start at zero, but at 1.

     

Source

This is a small pseudo-code that you can adapt to correctly assemble the information in your Excel file:

// Aquí asumo que "Jira.size()" son la cantidad de datos.
int cantidadFilas = Jira.size();

// En tu consulta MySQL "en el SELECT" estás usando 5 columnas.
int cantidadColumnas = 5;   
// Armar una tabla en Excel. Esto se hace
// recorriendo las columnas y las filas.
// El primer ciclo "for" recorre la cantidad de datos.
for (int i = 1; i < cantidadFilas; i++) {

    // Crear una nueva fila.
    row = sheet.createRow(i);

    // El siguiente ciclo "for" recorre las columnas.
    for (int j = 1; j < cantidadColumnas; j++) {

        // Aquí armas la celda en la columna y fila específica.
        cell = row.createCell(j);
        cell.setCellValue(Jira[i]);
    }
}
    
answered by 08.02.2017 в 17:39