String areaCode=request.getParameter("areaCode"); List users=new ArrayList(); users=baseAreaManager.getSchoolUserByAreaCode(areaCode); String areaName = baseAreaManager.getLabelByCode(areaCode); String downloadUrl=""; String title=""; try { //response.setHeader("Content-Disposition", "attachment; filename=" + new String(("导出学校管理员账号表.xls").getBytes("GBK"),"ISO8859-1")); //ExcelUtil.WriteExcel(request.getSession().getServletContext().getRealPath("/excel/schoolusers.xls"), // "学校管理员账号表", 2, users, response.getOutputStream()); //} catch (Exception e) { // e.printStackTrace(); ///} title=areaName+"-直管学校管理员账号表"; String url=request.getSession().getServletContext().getRealPath("/"); String templatePath=url+"excel/"+title+".xls"; downloadUrl="/excel/"+title+".xls"; File ff = new File(templatePath); if(!ff.exists()) { ff.createNewFile(); } WorkbookSettings settings = new WorkbookSettings(); settings.setWriteAccess(null); WritableWorkbook wwb = Workbook.createWorkbook(ff,settings); WritableSheet wws = wwb.createSheet(title, 0); WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.NO_BOLD); WritableCellFormat cellFormat = new WritableCellFormat(font,NumberFormats.TEXT); WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"), 14, WritableFont.NO_BOLD); WritableCellFormat cellFormat1 = new WritableCellFormat(font1,NumberFormats.TEXT); cellFormat.setWrap(true); cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); cellFormat.setAlignment(jxl.format.Alignment.CENTRE); cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN); cellFormat1.setWrap(true); cellFormat1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); cellFormat1.setAlignment(jxl.format.Alignment.CENTRE); cellFormat1.setBorder(Border.ALL, BorderLineStyle.THIN); Label label = new Label(0, 0, title, cellFormat1); wws.addCell(label); //设置Excel表头 String[] titles=new String[]{"姓名","账号","学校","地区名称"}; for (int i = 0; i < titles.length; i++) { Label excelTitle = new Label(i, 1, titles[i], cellFormat1); wws.addCell(excelTitle); } for (int i = 0; i < users.size(); i++) { Object[] baseUser=(Object[])users.get(i); wws.setRowView(i+2, 270); wws.addCell(new Label(0, i + 2, baseUser[0].toString(), cellFormat)); wws.setColumnView(0, 28); wws.addCell(new Label(1, i + 2, baseUser[1].toString(), cellFormat)); wws.setColumnView(1, 18); wws.addCell(new Label(2, i + 2, baseUser[4].toString(), cellFormat)); wws.setColumnView(2, 38); wws.addCell(new Label(3, i + 2, baseUser[3].toString(), cellFormat)); wws.setColumnView(3, 18); } wws.mergeCells( 0 , 0 , 3 , 0 ); wwb.write(); wwb.close(); } catch (Exception e) { e.printStackTrace(); } response.setContentType("text/html;charset=utf-8"); java.io.PrintWriter pw = null; String returnValue = downloadUrl+"#"+title; try { pw = response.getWriter(); pw.write(returnValue); } catch (java.io.IOException e) { returnValue = "0"; e.printStackTrace(); } return null; |
|