java excel

public Sheet getSheet(MultipartFile mt) throws IOException {
        // 获取文件名
        String fileName = mt.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 用当前时间作为文件名
        final File excelFile = File.createTempFile(TimeUtils.getCurTime(), prefix);
        // 将multipartfile 转换为file
        mt.transferTo(excelFile);

        // 判断是否为excel类型文件
        if (!prefix.endsWith(".xls") && !prefix.endsWith(".xlsx")) {
            throw new GlobalFaceSignException(ResponseCodeEnum.FILE_FORMAT_ERROR);
        }

        FileInputStream fis = null;
        Workbook workbook = null;
        //获取文件
        try {
            fis = new FileInputStream(excelFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //得到工作簿
        try {
            if (prefix.endsWith(".xls")) {
                workbook = new HSSFWorkbook(fis);
            } else if (prefix.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(fis);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //得到一个工作表
        Sheet sheet = workbook.getSheetAt(0);
        //获得表头
        Row rowHead = sheet.getRow(0);
        //判断表头是否正确
        if (rowHead.getPhysicalNumberOfCells() == 0) {
            throw new GlobalFaceSignException(ResponseCodeEnum.HEADERS_NUMBER_INCORRECT);
        }
        return sheet;
    }
Sheet sheet = userService.getSheet(multipartFile);
        //获得数据的总行数
        int totalRowNum = sheet.getLastRowNum();

        //获得所有数据
        List<BankUserQuery> list = new ArrayList<>();
        for (int i = 1; i <= totalRowNum; i++) {
            //获得第i行对象
            Row row = sheet.getRow(i);

            //一共5列,第6列有数据说明模板不对,就报错
            Cell cell6 = row.getCell((short) 5);
            if (cell6 != null) {
                throw new GlobalFaceSignException(ResponseCodeEnum.CONYENT_TEMPLATE_NOT_MATCH);
            }

            //获得营销代码list
            Cell cell4 = row.getCell((short) 3);
            if (cell4 == null){
                throw new GlobalFaceSignException(ResponseCodeEnum.PARAM_ERROR,"第"+ i +"行第4列缺少数据");
            }
            cell4.setCellType(CellType.STRING);
            String codes = String.valueOf(cell4.getStringCellValue());
            List<String> codeList = Arrays.asList(codes.split(","));
            for (String code : codeList) {
                if (!businessList.contains(code)){
                    throw new GlobalFaceSignException(ResponseCodeEnum.BUSINESS_TYPE_CODE_ERROR,"第"+ i +"行营销代码该银行未绑定");
                }
            }
            //获得账号名
            Cell cell = row.getCell((short) 0);
            if (cell == null){
                throw new GlobalFaceSignException(ResponseCodeEnum.PARAM_ERROR,"第"+ i +"行第1列缺少数据");
            }
            cell.setCellType(CellType.STRING);
            String account = String.valueOf(cell.getStringCellValue());
            if (StringUtils.isEmpty(account)){
                throw new GlobalFaceSignException(ResponseCodeEnum.PARAM_ERROR,"第"+ i +"行第1列缺少数据");
            }
            //获得姓名
            Cell cell2 = row.getCell((short) 1);
            if (cell2 == null){
                throw new GlobalFaceSignException(ResponseCodeEnum.PARAM_ERROR,"第"+ i +"行第2列缺少数据");
            }
            cell2.setCellType(CellType.STRING);
            String username = String.valueOf(cell2.getStringCellValue());
            if (StringUtils.isEmpty(username)){
                throw new GlobalFaceSignException(ResponseCodeEnum.PARAM_ERROR,"第"+ i +"行第2列缺少数据");
            }
            //获得手机号
            Cell cell3 = row.getCell((short) 2);
            if (cell3 == null){
                throw new GlobalFaceSignException(ResponseCodeEnum.PARAM_ERROR,"第"+ i +"行第3列缺少数据");
            }
            cell3.setCellType(CellType.STRING);
            String phone = String.valueOf(cell3.getStringCellValue());
            if (StringUtils.isEmpty(phone)){
                throw new GlobalFaceSignException(ResponseCodeEnum.PARAM_ERROR,"第"+ i +"行第3列缺少数据");
            }
            if (phone.length() != 11 || !phone.startsWith("1")){
                throw new GlobalFaceSignException(ResponseCodeEnum.PARAM_ERROR,"第"+ i +"行手机号格式错误");
            }
            //获得启用状态
            Cell cell5 = row.getCell((short) 4);
            if (cell5 == null){
                throw new GlobalFaceSignException(ResponseCodeEnum.PARAM_ERROR,"第"+ i +"行第5列缺少数据");
            }
            cell5.setCellType(CellType.STRING);
            String status = String.valueOf(cell5.getStringCellValue());
            if ("是".equals(status)){
                status = "1";
            }else if("否".equals(status)){
                status = "0";
            }else {
                throw new GlobalFaceSignException(ResponseCodeEnum.PARAM_ERROR,"第"+ i +"行只能输入是或否");
            }
            BankUserQuery userAddQuery = new BankUserQuery();
            userAddQuery.setBankId(attrCode);
            userAddQuery.setUserId(account);
            userAddQuery.setUsername(username);
            userAddQuery.setPhone(phone);
            userAddQuery.setRole(Collections.singletonList(roleId.toString()));
            userAddQuery.setCode(codeList);
            userAddQuery.setStatus(status);
            //加入列表
            list.add(userAddQuery);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值