Foros del Web » Programación para mayores de 30 ;) » Java »

[SOLUCIONADO] Crear Excel POI de BD

Estas en el tema de Crear Excel POI de BD en el foro de Java en Foros del Web. Hola... una consulta :) quiero crear un excel desde datos de la BD, pero me encuentro con un problema al traer los datos de l ...
  #1 (permalink)  
Antiguo 26/04/2013, 05:41
Avatar de el_java  
Fecha de Ingreso: enero-2008
Mensajes: 185
Antigüedad: 16 años, 3 meses
Puntos: 3
Crear Excel POI de BD

Hola... una consulta :)


quiero crear un excel desde datos de la BD, pero me encuentro con un problema al traer los datos de l BD que no se como funciona :/
Utilizo POI


aca la cabecera y en donde se exporta el archivo excel .xlsx

Código C:
Ver original
  1. XSSFWorkbook wb = new XSSFWorkbook();;
  2.        
  3.         response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  4.         response.setHeader("Content-Disposition", "attachment; filename=ExcelPrueba.xlsx");
  5.        
  6.         Sheet sheet = wb.createSheet("MiHoja");
  7.        
  8.        
  9.         /*Estilos*/
  10.         XSSFFont font = wb.createFont();
  11.         font.setBoldweight((short) 700);
  12.         sheet.setDefaultColumnWidth(15);
  13.        
  14.         XSSFCellStyle headerStyle = wb.createCellStyle();
  15.         headerStyle.setFont(font);
  16.        
  17.         XSSFCellStyle dataStyle = wb.createCellStyle();
  18.         dataStyle.setWrapText(true);
  19.         /*Fin Estilos*/
  20.        
  21.        
  22.         /*Fila Cabecera*/
  23.         Row row = sheet.createRow(0);
  24.        
  25.         Cell cell1 = row.createCell(0);
  26.         cell1.setCellStyle(headerStyle);
  27.         cell1.setCellValue("Cuenta");
  28.        
  29.         Cell cell2 = row.createCell(1);
  30.         cell2.setCellStyle(headerStyle);
  31.         cell2.setCellValue("Ficha");
  32.        
  33.         Cell cell3 = row.createCell(2);
  34.         cell3.setCellStyle(headerStyle);
  35.         cell3.setCellValue("Nombre Publicar");
  36.        
  37.         Cell cell4 = row.createCell(3);
  38.         cell4.setCellStyle(headerStyle);
  39.         cell4.setCellValue("Calle");
  40.        
  41.         Cell cell5 = row.createCell(4);
  42.         cell5.setCellStyle(headerStyle);
  43.         cell5.setCellValue("Numero");
  44.        
  45.         Cell cell6 = row.createCell(5);
  46.         cell6.setCellStyle(headerStyle);
  47.         cell6.setCellValue("Resto");
  48.        
  49.         Cell cell7 = row.createCell(6);
  50.         cell7.setCellStyle(headerStyle);
  51.         cell7.setCellValue("Telfono");
  52.        
  53.         Cell cell8 = row.createCell(7);
  54.         cell8.setCellStyle(headerStyle);
  55.         cell8.setCellValue("Zona Telefono");
  56.         /*FIN FILA CABECERA*/
  57.        
  58.         /*DATOS PARA MOSTRAR*/
  59.         CallCenterHome home = (CallCenterHome) ServiceLocator.getHome("contract/callCenter/CallCenter");
  60.        
  61.         CallCenterRemote callCenterRemote = home.create();
  62.         Collection result = new ArrayList();
  63.        
  64.        
  65.         Integer filterStatus = new Integer(-1);
  66.     Integer filterCuenta = new Integer(0);
  67.        
  68.         result = callCenterRemote.getCallCenter(filterStatus, filterCuenta);
  69.        
  70.         int i = 1;
  71.         Iterator it = result.iterator();
  72.        
  73.    
  74.         while(it.hasNext()) {
  75.             System.out.println(it.next());
  76.           /*Aca no se como recorrer la coleccion para traer los datos y llenar las celdas*/
  77.            //  sheet.createRow(i).createCell(i).setCellValue((String)it.next());
  78.             i++;
  79.         }
  80.  
  81.        
  82.         OutputStream out = response.getOutputStream();
  83.         wb.write(out);
  84.         out.flush();
  85.         out.close();


aca una parte del archivo en donde genera la consulta a la BD


Código C:
Ver original
  1. StringBuffer select =
  2.                     new StringBuffer("select CO_CUENTA, ")
  3.                     .append("        CALL_CENTER, ")
  4.                     .append("        CO_FICHA, ")
  5.                     .append("        NOMBRE_PUBLICAR, ")
  6.                     .append("        CALLE, ")
  7.                     .append("        NUMERO, ")
  8.                     .append("        RESTO, ")
  9.                     .append("        ZONA_TELEFONO, ")
  10.                     .append("        TELEFONO, ")
  11.                     .append("        MAIL, ")
  12.                     .append("        GRABACION, ")
  13.                     .append("        ESTADO ")
  14.                     .append(" from edithor.call_center_gest ")
  15.                     .append(p)
  16.                     .append(p2)
  17.                     .append(" order by CO_CUENTA");
  18.             quickSQL = new QuickSQL(con);
  19.             QuickSQLResult[] quickSQLResult = quickSQL.executeQuery(select.toString(), param);
  20.  
  21.             Collection result = new ArrayList();
  22.             for(int i = 0; i < quickSQLResult.length; i++) {
  23.                 CallCenterVO rejectionVO = new CallCenterVO();
  24.                 rejectionVO.setCodCallCenter(quickSQLResult[i].getString("CALL_CENTER"));
  25.                 rejectionVO.setCoCuenta(quickSQLResult[i].getString("CO_CUENTA"));
  26.                 rejectionVO.setCoFicha(quickSQLResult[i].getString("CO_FICHA"));
  27.                 rejectionVO.setNombrePublicar(quickSQLResult[i].getString("NOMBRE_PUBLICAR"));
  28.                 rejectionVO.setCalle(quickSQLResult[i].getString("CALLE"));
  29.                 rejectionVO.setNumero(quickSQLResult[i].getString("NUMERO"));
  30.                 rejectionVO.setResto(quickSQLResult[i].getString("RESTO"));
  31.                 rejectionVO.setZonaTelefono(quickSQLResult[i].getString("ZONA_TELEFONO"));
  32.                 rejectionVO.setTelefono(quickSQLResult[i].getString("TELEFONO"));
  33.                 rejectionVO.setMail(quickSQLResult[i].getString("MAIL"));
  34.                 rejectionVO.setGrabacion(quickSQLResult[i].getString("GRABACION"));
  35.                 rejectionVO.setEstado(quickSQLResult[i].getString("ESTADO"));
  36.                 result.add(rejectionVO);
  37.             }
  38.             return result;


si pudiesen ayudar seria genial... alomejor estoy haciendo algo muy mal :(
__________________
La Mejor banda de Rock Chileno : Los Bunkers

Última edición por el_java; 26/04/2013 a las 06:31
  #2 (permalink)  
Antiguo 27/04/2013, 06:34
 
Fecha de Ingreso: octubre-2010
Mensajes: 154
Antigüedad: 13 años, 6 meses
Puntos: 5
Respuesta: Crear Excel POI de BD

probaste debugiarlo a ver donde salta el problema??
  #3 (permalink)  
Antiguo 29/04/2013, 06:52
Avatar de el_java  
Fecha de Ingreso: enero-2008
Mensajes: 185
Antigüedad: 16 años, 3 meses
Puntos: 3
Respuesta: Crear Excel POI de BD

Cita:
Iniciado por lucho248 Ver Mensaje
probaste debugiarlo a ver donde salta el problema??
ahora lo que he logrado es traer los datos de la BD, pero imprime direcciones de memoria...

br.com.tpi.contract.asdf.asdfVO@154c08a
__________________
La Mejor banda de Rock Chileno : Los Bunkers

Última edición por el_java; 29/04/2013 a las 07:00
  #4 (permalink)  
Antiguo 06/05/2013, 06:48
Avatar de el_java  
Fecha de Ingreso: enero-2008
Mensajes: 185
Antigüedad: 16 años, 3 meses
Puntos: 3
Respuesta: Crear Excel POI de BD

Alguien????
:(
__________________
La Mejor banda de Rock Chileno : Los Bunkers
  #5 (permalink)  
Antiguo 06/05/2013, 11:02
Avatar de Fuzzylog  
Fecha de Ingreso: agosto-2008
Ubicación: En internet
Mensajes: 2.511
Antigüedad: 15 años, 8 meses
Puntos: 188
Respuesta: Crear Excel POI de BD

System.out.println(it.next()); => Estás intentando imprimir por pantalla un OBJETO?

Pues eso es lo que te pasará.

Deberías hacer MiClase miObjeto = (MiClase)it.next();

o si usases generics tenerlo así:

Collection<MiCLase> result = new ArrayList<MiCLase>();
Iterator<MiCLase> it = result.iterator();
...
MiClase miObjeto = it.next();
System.out.println("Mostramos una propiedad String o convertible de miObjeto: " + miObjeto.getNombre());

Normalmente para trazas de log o debuguear o cosas desas se suele sobreescribir el método toString donde se muestran todos los atributos del objeto en pares NombreAtributo: valorAtributo.
__________________
if (fuzzy && smooth) {
fuzzylog = "c00l";
return true;
}
  #6 (permalink)  
Antiguo 06/05/2013, 16:16
Avatar de el_java  
Fecha de Ingreso: enero-2008
Mensajes: 185
Antigüedad: 16 años, 3 meses
Puntos: 3
Respuesta: Crear Excel POI de BD

Acabo de arreglarlo... sabia que tenia que sacar los datos con un getter... pero no sabìa como...


Código HTML:
Ver original
  1. while(it.hasNext()) {
  2.            
  3.             CallCenterVO rejectionVO = new CallCenterVO();
  4.             rejectionVO = (CallCenterVO) it.next();
  5.            
  6.             Row fila = sheet.createRow(i);
  7.             fila.createCell(0).setCellStyle(dataStyle);
  8.             fila.createCell(0).setCellValue(rejectionVO.getCoCuenta());
__________________
La Mejor banda de Rock Chileno : Los Bunkers

Etiquetas: bd, excel, poi, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:31.