-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi, I have a method to export from listbox to csv.
When I export my listbox have 66 rows but when I open the spreadsheet, it has the lines but empty (only the lines that seen in the listbox has text).
I read other posts and try to render all objects, but unsuccessful. i try put a timmer too but the same.
This is the coede, I hope that someone can see something.
public static void exportarExcel(final Listbox lb, final String nombreFichero){ //Tenemos que crear un timer para que le de tiempo a renderizar todos los items que hay en //el listbox. Sin el timer solo le daria tiempo a renderizar los items que hay en las paginas //del listbox a las que se ha accedido. final Timer timer = new Timer(1); timer.setRepeats(false);//Para que solo lanze el timer una vez timer.setRunning(false);//Para que no se inicia hasta que se lo digamos //Como es el timer de zk lo tenemos que añadir a la pagina. Lo añadimos a la primera celda //del listbox. timer.setParent(lb.getItems().get(0).getFirstChild()); //Creamos el evento que se llamara al iniciar el timer timer.addEventListener(Events.ON_TIMER, new EventListener<Event>() { public void onEvent(Event event) throws Exception { Exporta(lb, nombreFichero); timer.detach();//Destruimos el timer } }); //Se renderizan todos los items del listbox // lb.renderAll(); //Se inicia el timer para que de tiempo de renderizar todos los items timer.start(); } /** * Método para exportar un listbox pasado por parametro con los estilos definidos de: * Cabecera Azul marino con letra blanca en negrita * Cuerpo con el formateo de celdas (letra y alineacion) * Pie con un azul marino claro, letra blanca y negrita * @param lb el listbox que se debe pasar a la hoja de calculo * @param nomeFile el nombre del archivo que se generará * @throws IOException */ public static void Exporta(Listbox lb, String nomeFile) throws IOException { HSSFWorkbook workbook = new HSSFWorkbook(); //Nombre de la pagina de csv HSSFSheet sheet = workbook.createSheet("NewSheet"); HSSFRow row = sheet.createRow(0); //Cabeceras //Creammos la fuente de las cabeceras HSSFFont estiloFuenteCabeceras = workbook.createFont(); // Creamos los estilos de celdas. A estos se les tiene que anyadir el estilo de la fuente HSSFCellStyle estiloCeldaCabecera = workbook.createCellStyle(); //Aplicamos el estilo de fuente a los estilos de celdas de la cabeceras estiloCeldaCabecera.setFont(estiloFuenteCabeceras); //Indicamos el color de la cabecera estiloFuenteCabeceras.setColor(HSSFColor.WHITE.index); //Indicamos el color del fondo y la textura de la cabecera HSSFPalette paleta = workbook.getCustomPalette(); paleta.setColorAtIndex(HSSFColor.DARK_BLUE.index, (byte)0, (byte)51, (byte)102); estiloCeldaCabecera.setFillForegroundColor(HSSFColor.DARK_BLUE.index); estiloCeldaCabecera.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //Indicamos el estilo de la fuente de las cabeceras estiloFuenteCabeceras.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //TODO falta crear la fuente de las celdas // Recorremos las cabeceras y aplicamos el estilo que hemos preparado para las cabeceras int i = 0; row = sheet.createRow(0); for (Object head : lb.getHeads()) { if(head instanceof Listhead){ for (Object header : ((Listhead) head).getChildren()) { if(((Listheader) header).isVisible()){ String h = ((Listheader) header).getLabel(); HSSFCell cell = row.createCell(i); //aplicamos el estilo de la celda de cabecera cell.setCellStyle(estiloCeldaCabecera); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(h); i++; } } } } //Detalle int x = 1; int y = 0; int iter = 0; //Recorremos las filas del detalles System.out.println("nItems->" + lb.getItems().size()); for (Object item : lb.getItems()) { row = sheet.createRow(x); y = 0; iter++; //Recorremos las celdas del detalle for (Object lbCell : ((Listitem) item).getChildren()) { System.out.println("Analizando item->" + iter); if(((Listcell)lbCell).getListheader().isVisible()){ String h = ""; if(((Listcell) lbCell).getLabel().equals("")){ h = ((Listcell) lbCell).getTooltiptext(); }else{ h = ((Listcell) lbCell).getLabel(); } HSSFCell cell = row.createCell(y); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(h); y++; } } x++; } //Pie int j = 0; if(lb.getListfoot() != null){ row = sheet.createRow(x);//Creamos el pie //reemplazamos un color de la paleta por el color deseado a la hora de generar el pie paleta.setColorAtIndex(HSSFColor.BLUE.index, (byte)185, (byte)185, (byte)255); HSSFCellStyle estiloCeldaPie = workbook.createCellStyle(); estiloCeldaPie.setFillForegroundColor(HSSFColor.BLUE.index); //Recorremos las celdas del pie for (Object celda : lb.getListfoot().getChildren()) { if(celda instanceof Listfooter){ int columna = ((Listfooter) celda).getColumnIndex(); if(lb.getListhead().getChildren().get(columna).isVisible()){ HSSFCell cell = row.createCell(j); //aplicamos el estilo del pie cell.setCellStyle(estiloCeldaPie); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(((Listfooter) celda).getLabel()); j++; } } } } //Ajustamos las columnas int iteradorColumnas = 0; for (Object head : lb.getHeads()) { if(head instanceof Listhead){ for(Object header : ((Listhead) head).getChildren()){ sheet.autoSizeColumn(iteradorColumnas); iteradorColumnas++; } } } FileOutputStream fOut; try { fOut = new FileOutputStream(nomeFile+".csv"); // Write the Excel sheet workbook.write(fOut); fOut.flush(); // Done deal. Close it. fOut.close(); //Descargamos el archivo. File file = new File("DatosDerivaciones.csv"); org.zkoss.zul.Filedownload.save(file, "text/plain"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
thanks for all!
First of all you do not need POI to create csv file. Just open a file with .csv extension and write comma separated values to it. Later you can open this csv file with Excel and all comma separated values will will put into separate columns by Excel automatically.
Secondly, instead of iterating through Listbox children and accessing their label values, get the listbox model with Listbox.getModel() and iterate through the model just like any List collection.
I will try to make a sample if I have time today but it should be really very simple and straightforward.
Thanks ashishd.
I use POI to apply colors in the headers and footers and I haven't go any model in my listbox, I use beanding, but I think that the problem is why I have 66 blank rows.
Listbox needs an other render after load all?
Lot of thanks, regards
Marky
Asked: 2012-10-01 14:08:33 +0800
Seen: 170 times
Last updated: Nov 14 '12