Tema: POI con java
Ver Mensaje Individual
  #12 (permalink)  
Antiguo 08/05/2013, 06:50
lucho248
 
Fecha de Ingreso: octubre-2010
Mensajes: 154
Antigüedad: 13 años, 6 meses
Puntos: 5
Respuesta: POI con java

pasa que la clase entera es para dibujar el excel.
el excel es para un remito de envio de mercaderia...

lo que dibujo son las lineas, esquinas, estilos de letras, etc.

paso parte del codigo donde creo que es lo mas importante.

Código Javascript:
Ver original
  1. public class GenerarExcel {
  2.  
  3.     /*creo los atributos*/
  4.     HSSFWorkbook archivo = new HSSFWorkbook();
  5.     String ruta = "c://service/";
  6.     FileOutputStream fileOut;
  7.     Vector<Integer> listID;
  8.     HSSFCellStyle cs, csTitulo, csCentrado,  
  9.             csLGSup, csLGInf,
  10.             csLGDer, csLGIzq,
  11.             csLSup, csLInf, csLDer, csLIzq, csNegrita,
  12.             csTopIzq, csTopDer, csBottomDer, csBottomIzq;
  13.     Font fNegrita, fuente, fNTitulo;
  14.    
  15.     HSSFSheet hoja1;
  16.     HSSFRow remito;
  17.     String fecha;
  18.    
  19.     public GenerarExcel(Vector<Integer> listIDDatServ) throws IOException {
  20.         try {      
  21.             this.listID = listIDDatServ;
  22.         /* Carga los datos del excel de la ruta especificada* */
  23.             archivo = new HSSFWorkbook();
  24.             //tomo la primera hoja del archivo
  25.             hoja1 = archivo.createSheet("Hoja1");
  26.                 //seteo la hoja en tamaño A4 horizontal
  27.             hoja1.getPrintSetup().setLandscape(false);
  28.             hoja1.getPrintSetup().setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
  29.            
  30.             /** CARGO FILAS Y MARCOS DEL REMITO * */
  31.             this.creoFilas ();
  32.            
  33.             /**Creo los estilos a:*/
  34.             this.creoEstilos();
  35.             this.setMargen();
  36.            /** CARGO CAMPOS FIJOS DEL REMITO * */
  37.             this.camposFijos();
  38.             /** CARGO CAMPOS VARIABLES DEL REMITO * */
  39.             this.camposVariables();
  40.             /** CARGO MARCOS DEL REMITO * */
  41.             this.creoMarcos();     
  42.            
  43.             fileOut = new FileOutputStream(ruta);
  44.             archivo.write(fileOut);
  45.             fileOut.close();
  46.  
  47.         } catch (FileNotFoundException ex) {
  48.             JFrame frame = new JFrame();
  49.             JOptionPane.showMessageDialog(frame,
  50.                     "No se encontró el archivo \n" +
  51.                     "o el archivo ya se encuentra abierto.        ",
  52.                     "Error",
  53.                     JOptionPane.WARNING_MESSAGE);
  54.         } catch (IOException ex) {
  55.             JFrame frame = new JFrame();
  56.             JOptionPane.showMessageDialog(frame,
  57.                     "Error en la carga de datos.        ",
  58.                     "Error",
  59.                     JOptionPane.WARNING_MESSAGE);
  60.         }catch (Exception ex) {
  61.             JFrame frame = new JFrame();
  62.             JOptionPane.showMessageDialog(frame,
  63.                     "Se produjo un error y la APP se cerrará.        ",
  64.                     "Error",
  65.                     JOptionPane.WARNING_MESSAGE);
  66.         }
  67.     }
Código CreoFilas:
Ver original
  1. public void creoFilas (){
  2.         /**
  3.          * ANCHO COLUMNAS
  4.          * */
  5.         int i = hoja1.getColumnWidth(1);
  6.         //setColumnWidth(column, width);
  7.         hoja1.setColumnWidth(0, i/2); //col 0
  8.         hoja1.setColumnWidth(2, i/2); //col 10
  9.         hoja1.setColumnWidth(3, i*2); //col 10
  10.         hoja1.setColumnWidth(5, i/4); //col 4
  11.         hoja1.setColumnWidth(6, i/5); //col 5
  12. //      hoja1.setColumnWidth(7, i/2); //col 6
  13.         hoja1.setColumnWidth(11, i/2); //col 10
  14.        
  15. //      archivo.set
  16.         /**
  17.          * CREO FILAS
  18.          * */
  19.         Row r;
  20.         int y;
  21.         for (int f = 0; f<=64; f++){
  22.             hoja1.createRow(f);
  23.             for (int g = 0; g<=12; g++){
  24.                 hoja1.getRow(f).createCell(g);
  25.             }
  26.         }          
  27.     }
Código creoMarcos:
Ver original
  1. public void creoMarcos(){
  2.  
  3.         /**
  4.          * AGREGO MARCOS Y LINEAS
  5.          * */
  6.         int x, y, z;
  7.         x = 0; y = 12; z = 2;
  8.        
  9.         hoja1.getRow(0).getCell(0).setCellStyle(csTopIzq);
  10.         for (int i = 1; i<y; i++){
  11.             hoja1.getRow(0).getCell(i).setCellStyle(csLGSup);
  12.         }
  13.         hoja1.getRow(0).getCell(y).setCellStyle(csTopDer);
  14.         hoja1.getRow(0).getCell(6).setCellStyle(csTopIzq);
  15.        
  16.         Row f;
  17.         for (int i = 1; i<=8; i++){
  18.             f = hoja1.getRow(i);
  19.             f.getCell(x).setCellStyle(csLGIzq);
  20.             f.getCell(y).setCellStyle(csLGDer);
  21.         }
  22.        
  23.         hoja1.getRow(1).getCell(5).setCellStyle(csTopIzq);
  24.         hoja1.getRow(2).getCell(5).setCellStyle(csLGSup);
  25.         hoja1.getRow(1).getCell(6).setCellStyle(csTopDer);
  26.         hoja1.getRow(2).getCell(6).setCellStyle(csTopIzq);
  27.         hoja1.getRow(1).getCell(7).setCellStyle(csLGIzq);
  28.         for (int i = 3; i<=9; i++){
  29.             f = hoja1.getRow(i);
  30.             f.getCell(6).setCellStyle(csLGIzq);        
  31.         }
  32.  
  33.         hoja1.getRow(9).getCell(x).setCellStyle(csBottomIzq);
  34.         for (int i = 1; i<y; i++){
  35.             hoja1.getRow(9).getCell(i).setCellStyle(csLGInf);
  36.         }
  37.         hoja1.getRow(9).getCell(y).setCellStyle(csBottomDer);
  38.         hoja1.getRow(9).getCell(5).setCellStyle(csBottomDer);
  39.        
  40.         for (int i = 10; i<14; i++){
  41.             f = hoja1.getRow(i);
  42.             f.getCell(x).setCellStyle(csLGIzq);
  43.             f.getCell(y).setCellStyle(csLGDer);
  44.         }
  45.         hoja1.getRow(14).getCell(x).setCellStyle(csBottomIzq);
  46.         hoja1.getRow(14).getCell(y).setCellStyle(csBottomDer);
  47.  
  48.         for (int i = 1; i<y; i++){
  49.             hoja1.getRow(14).getCell(i).setCellStyle(csLGInf);
  50.         }      
  51.        
  52.         for (int i = 15; i<=18; i++){
  53.             f = hoja1.getRow(i);
  54.             f.getCell(x).setCellStyle(csLGIzq);
  55.             f.getCell(y).setCellStyle(csLGDer);
  56.         }
  57.        
  58.         hoja1.getRow(18).getCell(x).setCellStyle(csBottomIzq);
  59.         for (int i = 1; i<y; i++){
  60.             hoja1.getRow(18).getCell(i).setCellStyle(csLGInf);
  61.         }
  62.         hoja1.getRow(18).getCell(y).setCellStyle(csBottomDer);
  63.        
  64.         hoja1.getRow(19).getCell(x).setCellStyle(csLGIzq);
  65.         hoja1.getRow(19).getCell(y).setCellStyle(csLGDer);
  66.        
  67.         hoja1.getRow(20).getCell(x).setCellStyle(csBottomIzq);
  68.         hoja1.getRow(20).getCell(y).setCellStyle(csBottomDer);
  69.         for (int i = 1; i<y; i++){
  70.             hoja1.getRow(20).getCell(i).setCellStyle(csLGInf);
  71.         }      
  72.        
  73.         for (int i = 21; i<=53; i++){
  74.             f = hoja1.getRow(i);
  75.             hoja1.getRow(i).getCell(x).setCellStyle(csLGIzq);
  76.             hoja1.getRow(i).getCell(2).setCellStyle(csLGIzq);
  77.             hoja1.getRow(i).getCell(y).setCellStyle(csLGDer);
  78.         }
  79.        
  80.         f = hoja1.getRow(54);
  81.         for (int i = 1; i<y; i++){
  82.             f.getCell(i).setCellStyle(csLGInf);
  83.         }
  84.         f.getCell(2).setCellStyle(csBottomIzq);
  85.         f.getCell(x).setCellStyle(csBottomIzq);
  86.         f.getCell(y).setCellStyle(csBottomDer);
  87.  
  88.         f = hoja1.getRow(61);
  89.         f.getCell(x).setCellStyle(csLGInf);
  90.         f.getCell(x+1).setCellStyle(csLGInf);
  91.         f.getCell(x+2).setCellStyle(csLGInf);
  92.         f.getCell(x+3).setCellStyle(csLGInf);
  93.        
  94.         f.getCell(y-3).setCellStyle(csLGInf);
  95.         f.getCell(y-2).setCellStyle(csLGInf);
  96.         f.getCell(y-1).setCellStyle(csLGInf);
  97.         f.getCell(y).setCellStyle(csLGInf);
  98.        
  99.     }
Código creoEstilos:
Ver original
  1. public void creoEstilos(){
  2.         /*Creo los estilos a: */
  3.         csCentrado = archivo.createCellStyle();
  4.         csLSup = archivo.createCellStyle();
  5.         csLInf = archivo.createCellStyle();
  6.         csLDer = archivo.createCellStyle();
  7.         csLIzq = archivo.createCellStyle();
  8.         csLGDer = archivo.createCellStyle(); csLGIzq = archivo.createCellStyle();
  9.         csLGSup = archivo.createCellStyle(); csLGInf  = archivo.createCellStyle();
  10.        
  11.         csCentrado.setAlignment(CellStyle.ALIGN_CENTER);
  12.        
  13.         //font size 10
  14.           fuente = archivo.createFont();
  15.           fuente.setFontHeightInPoints((short) 10);
  16.          
  17.           fNTitulo = archivo.createFont();
  18.           fNTitulo.setFontHeightInPoints((short) 15);
  19. //        fNTitulo.setFont(HSSFFont.FONT_ARIAL); //Tipo ARIAL
  20.           fNTitulo.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //Negrita
  21.          
  22.           //Simple style
  23.           cs = archivo.createCellStyle();
  24.           cs.setFont(fuente);
  25.          
  26.           csTitulo = archivo.createCellStyle();
  27.           csTitulo.setFont(fNTitulo);
  28.          
  29.          
  30.         //NEGRITA
  31.         csNegrita = archivo.createCellStyle();
  32.         fNegrita = archivo.createFont();
  33.         fNegrita.setFontName(HSSFFont.FONT_ARIAL); //Tipo ARIAL
  34.         fNegrita.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //Negrita
  35.         fNegrita.setFontHeightInPoints((short)10); //tamaño 10     
  36.         csNegrita.setFont(fNegrita);
  37.        
  38.         //BORDES    //SUPERIOR
  39.         csLGSup.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
  40.                     //INFERIOR
  41.         csLGInf.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
  42.                     //DERECHA
  43.         csLGDer.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
  44.                     //DERECHA SUPERIOR
  45.         csTopDer = archivo.createCellStyle();
  46.         csTopDer.setBorderTop(CellStyle.BORDER_MEDIUM);
  47.         csTopDer.setTopBorderColor(IndexedColors.BLACK.getIndex());
  48.         csTopDer.setBorderRight(CellStyle.BORDER_MEDIUM);
  49.         csTopDer.setRightBorderColor(IndexedColors.BLACK.getIndex());
  50.         csTopDer.setFont(fuente);
  51.                     //DERECHA INFERIOR
  52.         csBottomDer = archivo.createCellStyle();
  53.         csBottomDer.setBorderBottom(CellStyle.BORDER_MEDIUM);
  54.         csBottomDer.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  55.         csBottomDer.setBorderRight(CellStyle.BORDER_MEDIUM);
  56.         csBottomDer.setRightBorderColor(IndexedColors.BLACK.getIndex());
  57.         csBottomDer.setFont(fuente);
  58.                     //IZQUIERDA
  59.         csLGIzq.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
  60.                     //IZQUIERDA SUPERIOR
  61.         csTopIzq = archivo.createCellStyle();
  62.         csTopIzq.setBorderTop(CellStyle.BORDER_MEDIUM);
  63.         csTopIzq.setTopBorderColor(IndexedColors.BLACK.getIndex());
  64.         csTopIzq.setBorderLeft(CellStyle.BORDER_MEDIUM);
  65.         csTopIzq.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  66.         csTopIzq.setFont(fuente);
  67.                     //IZQUIERDA INFERIOR
  68.         csBottomIzq = archivo.createCellStyle();
  69.         csBottomIzq.setBorderBottom(CellStyle.BORDER_MEDIUM);
  70.         csBottomIzq.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  71.         csBottomIzq.setBorderLeft(CellStyle.BORDER_MEDIUM);
  72.         csBottomIzq.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  73.         csBottomIzq.setFont(fuente);
  74.     }
estas son para dibujar el contorno del remito, esquinas e interlineado.
el lo que falta es lo que cargo las celdas con datos de la base de datos, que no es mas que
Código cargar una celda:
Ver original
  1. hoja1.getRow(numFila).getCell(numCelda).setCellValue("Valor a cargar en la celda");
  2.         hoja1.getRow(numFila).getCell(numCelda).setCellStyle(estilo a asignar a dicha celda);

saludos.