138.ssm 框架下 导出Excel

本文介绍了一种使用Java导出Excel的方法,并提供了详细的代码示例。从导入依赖开始,到前端请求及后端处理,直至最终生成Excel文件并提供下载,文章全面覆盖了整个流程。特别关注了如何在服务器端生成Excel表格并将其返回给浏览器。

注意:一定不能使用Ajax请求方式,否则 不报错,但是就是下载不下来

1.效果

fa784aac44dbb91982fecabf8cdb4335fa4.jpg

2.实现过程

2.1 导入jar

        <!-- 13.excel导出 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.10-FINAL</version>
        </dependency>
         <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
             <version>3.10-FINAL</version>
        </dependency>
       <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi</artifactId>
          <version>3.10-FINAL</version>
        </dependency>

551ce0c522528db8053992b62e684209ae3.jpg

2.2 前端请求

aec64d54bb8dca4fbc9bbeab775f3e2e4bb.jpg

2.3 后台

2.3.1 controller

/**
     * 6.查询所有员工信息 在职在前 离职在后
     * @Title: exportEmpoyeeInfo
     * @Description: 
     * @return void
     * @throws IOException 
     * @throws 
       @date 2018年6月14日 下午5:15:19
     */
    @RequestMapping("/exportEmpoyeeInfo.action")
    public void exportEmpoyeeInfo(HttpServletResponse response ) throws IOException{
        response.setCharacterEncoding("UTF-8");  
        List<User> list =    employeeService.queryAllEmployeeInfoToExcel();
        System.out.println(list);
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("员工信息表");
        
        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("姓名");
        titleRow.createCell(1).setCellValue("爱好");
        titleRow.createCell(2).setCellValue("工作年限");
        titleRow.createCell(3).setCellValue("电话号码");
        titleRow.createCell(4).setCellValue("身份证号码");
        titleRow.createCell(5).setCellValue("户籍");
        titleRow.createCell(6).setCellValue("入职时间");
        titleRow.createCell(7).setCellValue("所在部门");
        titleRow.createCell(8).setCellValue("职位");
        titleRow.createCell(9).setCellValue("状态    ");
        titleRow.createCell(10).setCellValue("管理员");
        titleRow.createCell(11).setCellValue("其他描述");
        //遍历将数据放到excel列中
                for (User user : list) {
                    HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
                    dataRow.createCell(0).setCellValue(user.getUsername());
                    dataRow.createCell(1).setCellValue(user.getHobby());
                    dataRow.createCell(2).setCellValue(user.getWorkage());
                    dataRow.createCell(3).setCellValue(user.getPhonenumber());
                    dataRow.createCell(4).setCellValue(user.getPersoncardnumber());
                    dataRow.createCell(5).setCellValue(user.getAddress());
                    dataRow.createCell(6).setCellValue(user.getCreatetime());
                    dataRow.createCell(7).setCellValue(user.getDepartment());
                    dataRow.createCell(8).setCellValue(user.getJob());
                    dataRow.createCell(9).setCellValue(user.getStatus());
                    dataRow.createCell(10).setCellValue(user.getIsadmin());
                    dataRow.createCell(11).setCellValue(user.getOther());
                }
                  /*   // 设置下载时客户端Excel的名称  
             String filename =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ".xls";  
                response.setContentType("application/vnd.ms-excel");  
                response.setHeader("Content-disposition", "attachment;filename=" + filename);  */

            // 设置下载时客户端Excel的名称   (上面注释的改进版本,上面的中文不支持)
                    response.setContentType("application/octet-stream;charset=utf-8");
                    response.setHeader("Content-Disposition", "attachment;filename="
                            + new String("在职员工名单".getBytes(),"iso-8859-1") + ".xls");


                OutputStream ouputStream = response.getOutputStream();  
                wb.write(ouputStream);  
                ouputStream.flush();  
                ouputStream.close();
    }

7511c48c1166ff56a383635f011bc12af49.jpg

12c590a3e3db4919c47616c0f2f40ad38c1.jpg

2.3.2 service

2.3.2.1 接口

cf420f59c59de5a7d8c28faa55a28e92836.jpg

2.3.2.2 实现类

8edafc48ea2d0af8c90526bc4ec92d8a3a6.jpg

2.3.3 mapper

2.3.3.1 接口

2611f8c2a493f9625bf26df1cf55e4f3e82.jpg

2.3.3.2 映射文件、

a0e7d56600025b17cb5fc198405dc662f39.jpg

 

转载于:https://my.oschina.net/springMVCAndspring/blog/1830460

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值