<a href="javascript:excels()" class="coolbg"> 导出Excel </a>
js里面
function excels() { $.ajax({ type:"GET", url:"${pageContext.request.contextPath}/pro/excel", success:function (msgs) { //成功的话弹出提示框 if (confirm("确定要导出到excel吗?")){ alert(msgs.success) } } }) }
控制层
//从数据库导出excel表格 @RequestMapping(value = "/excel",method = RequestMethod.GET) @ResponseBody public Map<String,Object> exportExcel(){ List<Project> list = projectService.getAll(); Map<String,Object> map = new HashMap<>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("sheet1"); Row row = sheet.createRow(0); Cell[] cell = new HSSFCell[10]; for (int i = 0; i <cell.length ; i++) { cell[i]=row.createCell(i); } cell[0].setCellValue("序号"); cell[1].setCellValue("项目名称"); cell[2].setCellValue("客户公司名称"); cell[3].setCellValue("客户方负责人"); cell[4].setCellValue("项目经理"); cell[5].setCellValue("开发人员数"); cell[6].setCellValue("开始时间"); cell[7].setCellValue("立项时间"); cell[8].setCellValue("计划完成时间"); cell[9].setCellValue("状态"); for (int i = 0; i <list.size(); i++) { Project project = list.get(i); Row row1 = sheet.createRow(i + 1); Cell[] cell1 = new HSSFCell[10]; for (int j = 0; j <cell1.length ; j++) { cell1[j]=row1.createCell(j); } cell1[0].setCellValue(i+1); cell1[1].setCellValue(project.getPname()); cell1[2].setCellValue(project.getCustomer().getComname()); //因为数据库存得int类型 需要关联客户表去客户表中找。 cell1[3].setCellValue(project.getComper()); cell1[4].setCellValue(project.getEmployee().getEname());//员工表的ename就是项目表的外键。员工表的ename就是项目表的项目经理。 cell1[5].setCellValue(project.getEmpcount()); cell1[6].setCellValue(sdf.format(project.getStarttime())); cell1[7].setCellValue(sdf.format(project.getBuildtime())); cell1[8].setCellValue(sdf.format(project.getEndtime())); cell1[9].setCellValue("进行中"); } try { wb.write(new FileOutputStream(new File("C:\Users\19766\Desktop\project.xls"))); map.put("result",200); map.put("success","导入成功"); } catch (IOException e) { e.printStackTrace(); //失败的话页面回显函数使用 map.put("result",500); }finally { try { if (wb!=null){ wb.close(); } } catch (IOException e) { e.printStackTrace(); } } return map; }