Tema: Java y poi
Ver Mensaje Individual
  #4 (permalink)  
Antiguo 17/05/2011, 13:28
Avatar de jhonmelguizo
jhonmelguizo
 
Fecha de Ingreso: enero-2008
Ubicación: Medellín - Colombia
Mensajes: 264
Antigüedad: 16 años, 3 meses
Puntos: 6
Respuesta: Java y poi

Cita:
Iniciado por gorbejea Ver Mensaje
Respecto a lo mismo de antes, al ver que no puedo hacer nada, estoy haciendolo todo a mano, pero no se utilizar bien el tema de HSSFRow y HSSFCell.
Hola,

Yo lo hago de la siguiente manera:

Código JAVA:
Ver original
  1. /**
  2.      * Exporta un archivo de excel desde una consulta sql
  3.      * @param SQL
  4.      * @param nomArch
  5.      */
  6.     public void generarArchivo(String SQL, String nomArch){
  7.         try{
  8.         HSSFWorkbook wb = new HSSFWorkbook();
  9.         HSSFSheet sheet = wb.createSheet("Resultado Filtro");
  10.         HSSFRow rowhead = sheet.createRow(0);
  11.  
  12.        //Este es un objeto que implemente el cual se encarga de hacer la conexión13
  13.       //con el servidor x, retorna un ResultSet
  14.         BaseDatos Datos = new BaseDatos(SQL,true);
  15.        
  16.         for(int i = 0; i < Datos.getColumnCount(); i++){
  17.             rowhead.createCell(i).setCellValue(Datos.getColumnName(i));
  18.         }
  19.  
  20.         for(int fil=1; fil<Datos.getRowCount(); fil++){
  21.                 HSSFRow row = sheet.createRow(fil);
  22.             for(int col=0; col<Datos.getColumnCount(); col++){
  23.                 try{
  24.                     row.createCell(col).setCellValue(Datos.getValueAt(fil-1, col).toString());
  25.                 }catch(Exception e){
  26.                     row.createCell(col).setCellValue("");
  27.                 }
  28.             }
  29.         }
  30.  
  31.         FileOutputStream fileOut = new FileOutputStream("c:\\"+nomArch+".xls");
  32.         wb.write(fileOut);
  33.         fileOut.close();
  34.  
  35.         //Ejecutar archivo de excel
  36.         Runtime.getRuntime().exec("cmd /c start C:\\"+nomArch+".xls");
  37.  
  38.         }
  39.         catch(Exception e){
  40.             javax.swing.JOptionPane.showMessageDialog
  41.                     (null, "Error al exportar la consulta: " + e);
  42.         }
  43.     }

Espero te sirva.
__________________
¡No competimos, compartimos!...