152.使用ExcelUtils导出Excel

建议:使用ajax请求,因为这种发生是直接下载指定位置,使用Ajax回调函数给用户提示

 

原来导出Excel的方式:https://my.oschina.net/springMVCAndspring/blog/1830460

工具类及jar下载路径: https://gitee.com/Luck_Me/ExcelUtis/tree/master

参考文档:https://www.oschina.net/news/97902/excelutil-2-0-1-released

 

1. 效果 

80d39fd79c3aa9fd549fb7d259c278cc45c.jpg

2. 实现过程

2.1 导入依赖的jar

    <!-- 16.使用 execelUtils导出Excel表   这个是在上边第13个  改进的   如果使用 这个 第13个依赖的jar就不需要了 -->
        <dependency>
            <groupId>net.oschina.likaixuan</groupId>
            <artifactId>excelutil</artifactId>
            <version>2.0.1</version>
        </dependency>

6f888ba24e55652daca469d48c3f390a619.jpg

2.2  项目中导入 工具类

工具类内容完全不用改(你的包名要改

c448111c065d50529d8e53ca6e0db765f19.jpg

2.3 使用(有必要粘贴代码)

/**
     * 6.查询所有员工信息 在职在前 离职在后
     * @Title: exportEmpoyeeInfo
     * @Description: 
     * @return void
     * @throws Exception 
     * @throws 
       @date 2018年6月14日 下午5:15:19
     */
    @RequestMapping("/exportEmpoyeeInfo.action")
    public void exportEmpoyeeInfo(HttpServletResponse response ) throws Exception{
        response.setCharacterEncoding("UTF-8");  
        List<User> list =    employeeService.queryAllEmployeeInfoToExcel();
        /*//创建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());
                    String hobby = user.getHobby();
                    if(hobby.length()==0){
                        hobby="";
                    }else{
                        if("1".equals(hobby)){
                            hobby="散步";
                        }
                        if("2".equals(hobby)){
                            hobby="爬山";
                        }
                        if("3".equals(hobby)){
                            hobby="读书";
                        }
                        if("1,2".equals(hobby)){
                            hobby="散步,爬山";
                        }
                        if("1,3".equals(hobby)){
                            hobby="散步,读书";
                        }
                        if("2,3".equals(hobby)){
                            hobby="爬山,读书";
                        }
                        if("1,2,3".equals(hobby)){
                            hobby="散步,爬山,读书";
                        }
                    }
                    
                    dataRow.createCell(1).setCellValue(hobby);
                    String workage = user.getWorkage().toString();
                    if(workage.length()==0){
                        workage="";
                    }else{
                        if("0".equals(workage)){
                            workage="1年以内";
                        }
                        if("1".equals(workage)){
                            workage="1-3年";
                        }
                        if("2".equals(workage)){
                            workage="3-5年";
                        }
                        if("3".equals(workage)){
                            workage="5年以上";
                        }
                    }
                    dataRow.createCell(2).setCellValue(workage);
                    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.getDepartmentname());
                    dataRow.createCell(8).setCellValue(user.getJobname());
                    String status = user.getStatus();
                    if("0".equals(status)){
                        status="在职";
                    }else{
                        status="离职";
                    }
                    dataRow.createCell(9).setCellValue(status);
                    String isadmin = user.getIsadmin();
                    if("0".equals(isadmin)){
                        isadmin="是";
                    }else{
                        isadmin="否";
                    }
                    dataRow.createCell(10).setCellValue(isadmin);
                    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);  
                OutputStream ouputStream = response.getOutputStream();  
                wb.write(ouputStream);  
                ouputStream.flush();  
                ouputStream.close(); */
        Map<String, String> tip = new HashMap<String,String>();
        try {
            //列名 及属性  如:姓名(Excel的列名)  username(pojo属性)  “,”英文的
            String keyValue ="姓名:username,爱好:hobby,工作年限:workage,电话号码:phonenumber,入职时间:createtime"; 
            //获取 当前系统的桌面路径   记得 c:访问权限 要开放
            FileSystemView fsv = FileSystemView.getFileSystemView();
            File com=fsv.getHomeDirectory();    //这便是读取桌面路径的方法了
            String path = com.getPath();
            //调用工具类     cn.guang.ssm.pojo.User:实体类全路径
            ExcelUtil.exportExcel(path+"/工具导出Excel测试.xls",keyValue,list,"cn.guang.ssm.pojo.User");
            tip.put("tip", "导出成功!");
        } catch (Exception e) {
            e.printStackTrace();
            tip.put("tip", "导出失败!");
        }
        //将map转为json
            String jsonStri = JSONObject.toJSONString(tip);
            //将结果以流的形式输出到请求位置
            PrintWriter out=response.getWriter();
            out.println(jsonStri);
            out.flush();
            out.close();
    }

b86c2f31562683d24e139a30a9348259aca.jpg

 

de6e1ae955c24bd760737fc1087da661ec1.jpg

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值