`
Java_大猫
  • 浏览: 170528 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

jxl例子

    博客分类:
  • J2SE
阅读更多
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;
		}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics