public class ExcelReport {
public void createExcel(OutputStream os ,List<Book> list) throws Exception{
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet sheet = wwb.createSheet(" ", 0);
//
WritableCellFormat wcf = new WritableCellFormat();
wcf.setAlignment(Alignment.CENTRE);
wcf.setBackground(jxl.format.Colour.SEA_GREEN);
sheet.addCell(new Label(0, 0, " ",wcf));
sheet.addCell(new Label(1, 0, " ",wcf));
sheet.addCell(new Label(2, 0, " ",wcf));
sheet.addCell(new Label(3, 0, " ",wcf));
sheet.addCell(new Label(4, 0, " ",wcf));
sheet.addCell(new Label(5, 0, " ",wcf));
for(int i=1;i<list.size();i++){
sheet.addCell(new Label(0,i,list.get(i).getBookId()));
sheet.addCell(new Label(1,i,list.get(i).getBookName()));
sheet.addCell(new Label(2,i,list.get(i).getAuthor()));
sheet.addCell(new Label(3,i,list.get(i).getPrice()));
sheet.addCell(new Label(4,i,list.get(i).getCount()));
sheet.addCell(new Label(5,i,list.get(i).getPublish()));
}
wwb.write();
wwb.close();
os.close();
}
}
public class Test {
public static void main(String[] args) throws Exception {
List list = bulidList();
OutputStream os = new FileOutputStream("d:\\test2.xls");
ExcelReport excel = new ExcelReport();
excel.createExcel(os, list);
}
public static List bulidList(){
List list = new ArrayList();
for(int i=0;i<10;i++){
Book bo = new Book();
bo.setBookId("00"+i);
bo.setAuthor(" "+i);
bo.setBookName(" "+i+" ");
bo.setCount(i+" ");
bo.setPrice("10"+i);
bo.setPublish(" "+i);
list.add(bo);
}
return list;
}
}