poi基本导入

action代码

//导入用户列表
    public String importExcel(){
        //1、获取excel文件
        if(userExcel != null){
            //是否是excel文件
            if(userExcelFileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){
                //2、导入 传入excel文件和文件名
                userService.importExcel(userExcel, userExcelFileName);
            }
        }
        return "list";
    }

service代码

public void importExcel(File userExcel, String userExcelFileName) {
        try {
            //创建文件读取流
            FileInputStream fileInputStream = new FileInputStream(userExcel);
            //使用正则表达式确认文件是03版本还是07版本
            boolean is03Excel = userExcelFileName.matches("^.+\\.(?i)(xls)$");
            //1、读取工作簿
            Workbook workbook = is03Excel ? new HSSFWorkbook(fileInputStream):new XSSFWorkbook(fileInputStream);
            //2、读取工作表
            Sheet sheet = workbook.getSheetAt(0);
            //3、读取行
            if(sheet.getPhysicalNumberOfRows() > 2){
                User user = null;
                for(int k = 2; k < sheet.getPhysicalNumberOfRows(); k++){
                    //4、读取单元格
                    Row row = sheet.getRow(k);
                    user = new User();
                    //用户名
                    Cell cell0 = row.getCell(0);
                    user.setName(cell0.getStringCellValue());
                    //帐号
                    Cell cell1 = row.getCell(1);
                    user.setAccount(cell1.getStringCellValue());
                    //所属部门
                    Cell cell2 = row.getCell(2);
                    user.setDept(cell2.getStringCellValue());
                    //性别
                    Cell cell3 = row.getCell(3);
                    //性别
                    //这里性别是boolean类型所以使用equals来判断是男返回true
                    user.setGender(cell3.getStringCellValue().equals("男"));
                    //*手机号*
                    String mobile = "";
                    Cell cell4 = row.getCell(4);
                    try {
                        //excel中如果是string类型就正常导入
                        mobile = cell4.getStringCellValue();
                    } catch (Exception e) {
                        //出现异常代表是数字类型 数字类型由于数字过大会出现1.333....
                        //的情况,所以先使用获取数字方式在使用BigDecimal转换
                        double dMobile = cell4.getNumericCellValue();
                        mobile = BigDecimal.valueOf(dMobile).toString();
                    }
                    user.setMobile(mobile);

                    //电子邮箱
                    Cell cell5 = row.getCell(5);
                    user.setEmail(cell5.getStringCellValue());
                    //生日
                    Cell cell6 = row.getCell(6);
                    if(cell6.getDateCellValue() != null){
                        user.setBirthday(cell6.getDateCellValue());
                    }
                    //默认用户密码为 123456
                    user.setPassword("123456");
                    //默认用户状态为 有效
                    user.setState(User.USER_STATE_VALID);

                    //5、保存用户
                    save(user);
                }
            }
            workbook.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值