Monthly Archives: April 2014

Generic Excel Download in ZK Listbox


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.MouseEvent;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listhead;
import org.zkoss.zul.Listheader;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox.ClickEvent;


public class test {

 private List<Person> personList=new ArrayList<Person>();

 public List<Person> getPersonList() {
 return personList;
 }

 public void setPersonList(List<Person> personList) {
 this.personList = personList;
 }
 
 @AfterCompose
 public void afterCompose() {
 Person a=new Person();
 a.setAge(13);
 a.setBirthDate(new Date());
 a.setName("Burak");
 Person b=new Person();
 b.setAge(14);
 b.setBirthDate(new Date());
 b.setName("Veli");
 personList.add(a);
 personList.add(b);
 }
 
 @Command
 public void excel(@BindingParam("event") MouseEvent event) throws IOException
 {
 HSSFWorkbook workBook = new HSSFWorkbook();
 HSSFSheet workSheet = workBook.createSheet("Orders");
 Row excelRow = null;
 Cell excelCell = null;
 int rowNumber = 0;
 Component listbox=event.getTarget();
 while (!(listbox instanceof Listbox)) {
 listbox=listbox.getParent();
 }
 Listhead listHead = ((Listbox) listbox).getListhead();
 excelRow = workSheet.createRow(rowNumber++);
 for(int i=0;i<listHead.getChildren().size();i++)
 {
 Listheader listHeader = (Listheader) listHead.getChildren().get(i);
 
 excelCell = excelRow.createCell(i);
 excelCell.setCellValue(listHeader.getLabel());
 }
 for (int i=0; i < ((Listbox) listbox).getItems().size();i++) {
 Listitem item=((Listbox) listbox).getItems().get(i);
 excelRow = workSheet.createRow(rowNumber++);
 List<Listcell> cellList = item.getChildren();
 for(int k=0;k<cellList.size();k++) {
 excelCell = excelRow.createCell(k);
 excelCell.setCellValue(cellList.get(k).getLabel());
 }
 }
 List<Component> listBoxChildren = listbox.getChildren();
 File tempFile = new File("C:\\tempOrders.xls");
 FileOutputStream outputStream = new FileOutputStream(tempFile);
 workBook.write(outputStream);
 outputStream.close();
 Filedownload.save(tempFile, "application/file");
 }
 
}

<listbox model="@load(e.personList)" mold="paging" pageSize="1">
<auxhead>
 <auxheader><button label="Excel" sclass="pull-right" onClick="@command('excel', event=event)"></button></auxheader>
</auxhead>
<listhead>
 <listheader label="Ad"/>
 <listheader label="Yaş"/>
 <listheader label="Doğum"/>
</listhead>
<template name="model">
 <listitem>
 <listcell label="@load(each.name)"/>
 <listcell label="@load(each.age)"/>
 <listcell label="@load(each.birthDate)"/>
 </listitem>
</template>
</listbox>