`
liyinfeng56
  • 浏览: 91813 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java导出方法

阅读更多
//导出方法
 public void WriteToOutputStream(OutputStream out, String excelName,List list) throws Exception {
		WritableWorkbook workbook = Workbook.createWorkbook(out);
		WritableSheet sheet = workbook.createSheet(excelName, 0);
		WritableCellFormat ccf = new WritableCellFormat();

		ccf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
		ccf.setAlignment(jxl.format.Alignment.CENTRE);
		WritableCellFormat numcf = new WritableCellFormat(new NumberFormat(
				"#,##0.00"));
		numcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
		numcf.setAlignment(jxl.format.Alignment.RIGHT);

		WritableCellFormat zbcf = new WritableCellFormat();
		zbcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
		zbcf.setAlignment(jxl.format.Alignment.LEFT);		
		//设置列头
		sheet.addCell(new Label(0, 0, "名称", zbcf));
		sheet.addCell(new Label(1, 0, "性别", zbcf));
		sheet.addCell(new Label(2, 0, "地址", zbcf));
		sheet.setColumnView(0, 16);
	    //循环输出值
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);	
			String name =map.get("name").toString();
			sheet.addCell(new Label(0, i+1, String.valueOf(name), zbcf));			
			String sex = map.get("sex").toString();			
			sheet.addCell(new Label(1, i+1, String.valueOf(sex), zbcf));			
			String add = map.get("add").toString();	
			sheet.addCell(new Label(2, i+1, String.valueOf(add), zbcf));		
			
		}
		workbook.write();
		workbook.close();
	}



//action 调用
response.reset();
				response.setHeader("Content-Disposition", "attachment; filename=\""
						+ new String("个人信息导出".getBytes("gb2312"),
								"iso8859-1") + ".xls\"");
				
				export.WriteToOutputStream(response.getOutputStream(),"个人信息导出", list);
				response.setContentType("application/ms-excel;charset=gb2312");
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics