Problems with export csv

asked 2012-10-01 14:08:33 +0800

Marky gravatar image Marky flag of Spain
284 7

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.
		//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

	 * 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);
		//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
			//Indicamos el color de la cabecera
			//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);
			//Indicamos el estilo de la fuente de las cabeceras
			//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
		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;
			//Recorremos las celdas del detalle
			for (Object lbCell : ((Listitem) item).getChildren()) {
				System.out.println("Analizando item->" + iter);
					String h = "";
					if(((Listcell) lbCell).getLabel().equals("")){
							h = ((Listcell) lbCell).getTooltiptext();
						h = ((Listcell) lbCell).getLabel();
					HSSFCell cell = row.createCell(y);
		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();
			//Recorremos las celdas del pie
			for (Object celda : lb.getListfoot().getChildren()) {
				if(celda instanceof Listfooter){
					int columna = ((Listfooter) celda).getColumnIndex();
						HSSFCell cell = row.createCell(j);
						//aplicamos el estilo del pie
						cell.setCellValue(((Listfooter) celda).getLabel());
		//Ajustamos las columnas
		int iteradorColumnas = 0;
		for (Object head : lb.getHeads()) {
			if(head instanceof Listhead){
				for(Object header : ((Listhead) head).getChildren()){
		FileOutputStream fOut;
		try {
			fOut = new FileOutputStream(nomeFile+".csv");
			// Write the Excel sheet
			// Done deal. Close it.
			//Descargamos el archivo.
			File file = new File("DatosDerivaciones.csv");
			org.zkoss.zul.Filedownload.save(file, "text/plain");
		} catch (FileNotFoundException e) {
		} catch (IOException e) {

thanks for all!

delete flag offensive retag edit

3 Replies

Sort by » oldest newest

answered 2012-10-03 07:23:50 +0800

ashishd gravatar image ashishd flag of Taiwan
1972 6

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.

link publish delete flag offensive edit

answered 2012-10-03 16:02:25 +0800

Marky gravatar image Marky flag of Spain
284 7

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

link publish delete flag offensive edit

answered 2012-11-14 12:56:29 +0800

Neus gravatar image Neus
1415 14

Does anyone has an idea of why the items of this example are not rendered even using renderAll()?

Thank you

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools




Asked: 2012-10-01 14:08:33 +0800

Seen: 168 times

Last updated: Nov 14 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More