Java_大猫 发表于 2013-1-15 02:00:33

jxl例子

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;}}
页: [1]
查看完整版本: jxl例子