/** * 公共组件,导出Excel表格数据方法<p> * 使用方法: 数据为List<Object>类型,注意当中object的属性理论上必须为java基本数据类型 * 固然也支持像日期这种格式,即完整支持数据库中字段类型 * @author pery * @param className 数据模型Object的类完整名称 * ,eg:com.swust.kelab.Department * @param propertyName 数据模型类的属性对应的列名, * 如“depaId”对应列表“机构id“, * 未指定列名的属性不会被导出 * @param tableName 导出的excel中的表名称 * @param objectList 要导出的数据集 * @return 成功返回true,失败返回false; */
public static boolean exportExcel(String className, Map propertyName, String tableName, List<? extends Object> objectList, HttpServletResponse resp) throws IOException { resp.setCharacterEncoding("UTF-8"); resp.setContentType("application/x-download"); long now = System.currentTimeMillis(); String filedisplay = "导出用户信息"+now+".xls"; filedisplay = URLEncoder.encode(filedisplay, "UTF-8"); resp.addHeader("Content-Disposition", "attachment;filename="+ filedisplay); HSSFWorkbook wbook = new HSSFWorkbook(); HSSFSheet wsheet = wbook.createSheet(tableName); //表名 HSSFCellStyle cellStyle=getStyle(wbook); // 设置Excel表头 HSSFRow excelTitle = wsheet.createRow(0); excelTitle.setHeightInPoints(22); if(objectList.size()<=0){ wsheet.autoSizeColumn(0); HSSFCell titleCell = excelTitle.createCell(0); titleCell.setCellValue("对不起,没有可用数据导出"); titleCell.setCellStyle(cellStyle); } Class c = null; try { c = Class.forName(className); } catch (ClassNotFoundException e1) { System.out.println("传入反射用到的类名字有误"); e1.printStackTrace(); return false; } java.lang.reflect.Field[] flds = c.getDeclaredFields(); int j=0; for (int i = 0; i < flds.length; i++) { String columName=flds[i].getName(); if(propertyName.containsKey(columName)){ wsheet.setColumnWidth(j+1, (int)(100*35.7)); HSSFCell titleCell = excelTitle.createCell(j++); titleCell.setCellValue((String)propertyName.get(columName)); titleCell.setCellStyle(cellStyle); } } for (int i = 0; i < objectList.size(); i++) { Object obj = objectList.get(i); HSSFRow row = wsheet.createRow(i + 1); int k=0; for (j = 0; j < flds.length; j++) { String columName=flds[j].getName(); if(!propertyName.containsKey(columName)){ continue; } HSSFCell hssfCell = row.createCell(k++); try { flds[j].setAccessible(true); Object t = flds[j].get(obj); if(t instanceof Date){ t = dateFormat.format(t); } if(t!=null){ hssfCell.setCellValue(String.valueOf(t)); } } catch (IllegalArgumentException e) { System.out.println("该字段不是基本数据格式字段"); e.printStackTrace(); } catch (IllegalAccessException e) { System.out.println("非法访问"); e.printStackTrace(); } } } try { OutputStream out = resp.getOutputStream(); wbook.write(out); out.flush(); out.close(); } catch (FileNotFoundException e) { System.out.println("文件路径错误"); e.printStackTrace(); } catch (IOException e) { System.out.println("I/O错误"); e.printStackTrace(); } return true; }
controllerjava
public Object export(String username, String name, Integer state, HttpServletRequest request, HttpServletResponse response){ List<AdminEntity> adminEntityList = new ArrayList<>(); Map<String, Object> url = new HashMap<String, Object>(); ResultEntity resultEntity = new ResultEntity(); Integer status = 1; String message = null; AdminWebserviceService adminWebserviceService = new AdminWebserviceService(); Admin admin = adminWebserviceService.getAdminPort(); SoapHandler.handleSoap(admin); try { String operate = "导出用户信息"; addLog(operate, request); Map<String, Object> map = new HashMap<String, Object>(); map.put("id","用户ID"); map.put("username","登陆名"); map.put("name","真实姓名"); map.put("departmentTitle","部门名称"); map.put("roleTitle","角色"); map.put("mobile","电话"); map.put("email","邮箱"); map.put("createState","状态"); map.put("createAdmin","建立人"); map.put("createTime","建立时间"); List<CaAdmin> caAdminList = admin.getList2(username,name,state); for (CaAdmin caAdmin : caAdminList) { AdminEntity adminEntity = new AdminEntity(); CaRole caRole = admin.findRolesByUsername(caAdmin.getUsername()); CaDepartment caDepartment = admin.findDepartmentByDepartmentId(caAdmin.getDepartmentId()); //tools.converJavaBean 将class2中的属性值赋值给class1 tools.converJavaBean(adminEntity, caAdmin); adminEntity.setRoleTitle(caRole.getTitle()); adminEntity.setDepartmentTitle(caDepartment.getTitle()); if(caAdmin.getState()==1){ adminEntity.setCreateState("启用"); }else adminEntity.setCreateState("禁用"); adminEntityList.add(adminEntity); } //ExportAsExcel.exportExcel 导出到Excel Boolean a= ExportAsExcel.exportExcel("com.xxx.entity.AdminEntity",map,"用户信息表",adminEntityList,response); if(a){ //url.put("url",file); }else{ status = 0; message = "服务器错误"; } } catch (Exception e) { status = 0; message = "服务器错误"; } resultEntity.setStatus(status); resultEntity.setObj(url); resultEntity.setMessage(message); return resultEntity; }
结果输出一个文件流数据库