SpringMVC生成导出Excel表格

情景描述:

在页面查询出员工信息,点击”导出Excel“按钮,将员工信息生成Excel表格,并将Excel表格下载到本地。

/**
     * 将部门员工导出到Excel
     * @param ou  部门名称
     * @return
     */
    @RequestMapping("/exportExc.action")
    public void ExportExc(@RequestParam("ou")String ou,HttpServletRequest request,HttpServletResponse response){
        
        System.out.println("导出到Excel"+ou);
        
        //第一步:创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        
        //第二步:在webbook中添加一个sheet,对应Excel中的一个sheet
        HSSFSheet sheet = wb.createSheet(ou+"员工信息表");
        //第三步,在sheet中添加一个表头即第0行
        HSSFRow row = sheet.createRow(0);
        
           
        
        //第四步:创建单元格 ,并设置表表头居中
        
        HSSFCellStyle style= wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  //表头样式
        
        HSSFCell cell =row.createCell(0);
        cell.setCellValue("员工编号");
        cell.setCellStyle(style);
        sheet.setColumnWidth(0,4000);
        cell =row.createCell(1);
        cell.setCellValue("员工姓名");
        cell.setCellStyle(style);
        sheet.setColumnWidth(1,4000);
        cell =row.createCell(2);
        cell.setCellValue("部门");
        cell.setCellStyle(style);
        sheet.setColumnWidth(2,6000);
        cell =row.createCell(3);
        cell.setCellValue("岗位");
        cell.setCellStyle(style);
        cell =row.createCell(4);
        cell.setCellValue("岗位类型");
        cell.setCellStyle(style);
        cell =row.createCell(5);
        cell.setCellValue("是否在职");
        cell.setCellStyle(style);
        cell =row.createCell(6);
        cell.setCellValue("生日");
        cell.setCellStyle(style);
        cell =row.createCell(7);
        cell.setCellValue("性别");
        cell.setCellStyle(style);
        cell =row.createCell(8);
        cell.setCellValue("民族");
        cell.setCellStyle(style);
        cell =row.createCell(9);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell =row.createCell(10);
        cell.setCellValue("名字");
        cell.setCellStyle(style);
        cell =row.createCell(11);
        cell.setCellValue("密码");
        cell.setCellStyle(style);        
        
        //第五步:写入实体数据
        
        Department oneDepart = dService.getNextDepartment(ou);
        System.out.println("查出的部门:"+oneDepart);
        List<Employee> employees = oneDepart.getEmployees();
        System.out.println("待存入的员工信息:"+employees);
        //将员工对象存进单元行
        for (int i = 0; i < employees.size(); i++) {
            row= sheet.createRow(i+1);   //创建新的单元行
            Employee employee = employees.get(i);
            row.createCell(0).setCellValue(employee.getUid()); //创建一个单元格,存入uid
            row.createCell(1).setCellValue(employee.getCn());
            row.createCell(2).setCellValue(employee.getDepartment().getOu());
            row.createCell(3).setCellValue(employee.getJobtitlename());
            row.createCell(4).setCellValue(employee.getJobtypename());
            row.createCell(5).setCellValue(employee.getHrstatus());
            row.createCell(6).setCellValue(employee.getBirthday());
            row.createCell(7).setCellValue(employee.getSex());
            row.createCell(8).setCellValue(employee.getFolk());
            row.createCell(9).setCellValue(employee.getSn());
            row.createCell(10).setCellValue(employee.getGivenName());
            row.createCell(11).setCellValue(employee.getUserPassword());
            
        }
        //第六步,将文件存入指定路径
         try{

              // 设置response参数,可以打开下载页面
              String fileName=ou+"员工表.xls";
            /*  fileName = URLEncoder.encode(fileName, "utf-8");*/
              request.setCharacterEncoding("utf-8");
              response.setCharacterEncoding("utf-8");
              response.setContentType("application/x-download");
              response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
              
              OutputStream out = response.getOutputStream();
              wb.write(out);
              out.close();
              
          }catch(Exception e){
              e.printStackTrace();
          }
    }

之前用的ajax请求后台地址,一直没法下载,前台页面弹不出下载框
问题原因:
那是因为response原因,一般请求浏览器是会处理服务器输出的response,例如生成png、文件下载等,然而ajax请求只是个“字符型”的请求,json,text,html,xml,即请求的内容是以文本类型存放的。文件的下载是以二进制形式进行的,虽然可以读取到返回的response,但只是读取而已,是无法执行的,说白点就是js无法调用到浏览器的下载处理机制和程序。
解决方案:
    隐藏表单,用提交表单的形式
    用window.open() 或 window.location.href()
    创建iframe,iframe的src可以是文件地址url来请求后台并下载文件

 

参考文献:http://blog.csdn.net/maomao_0324/article/details/74931050

posted @ 2017-12-01 13:31  Actexpler  阅读(396)  评论(0编辑  收藏  举报