当前位置: 代码迷 >> Web前端 >> java导出步骤
  详细解决方案

java导出步骤

热度:100   发布时间:2012-08-21 13:00:22.0
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");
  相关解决方案