【liuyl】Java 读取excel数据

public ResponseEntity<Void> testImportByPersonName() {
        //名字重复
        HashMap<String, String> tempMap = new HashMap<>();
        //编号,名称
        HashMap<String, String> personMap = new HashMap<>();
        Workbook wb = null;
        Sheet sheet = null;
        Row row = null;
        String cellData = null;
        //Excel文件路径
        String filePath = "S:\\桌面\\OA人员工号整理.xls";
        //读取Excel文件
        try {
            wb = new HSSFWorkbook(new FileInputStream(filePath));
            //如果文件不为空
            if (wb != null) {
                //获取第一个sheet
                sheet = wb.getSheetAt(0);
                //获取最大行数
                int rownum = sheet.getPhysicalNumberOfRows();
                //获取第一行
                row = sheet.getRow(0);
                //获取最大列数
                int colnum = row.getPhysicalNumberOfCells();
                //循环行
                for (int i = 1; i < rownum; i++) {
                    row = sheet.getRow(i);
                    if (row != null) {
                        personMap.put(row.getCell(1).getStringCellValue(), row.getCell(0).getStringCellValue());
                    } else {
                        break;
                    }
                }
                System.out.println("数量:=" + personMap.size());
            }

            //遍历map
            Set<String> codes = personMap.keySet(); //所有人员编号
            for (String code : codes) {
                //根据编号获取名称
                String name = personMap.get(code);
                //查询person
                List<Person> personList = personRepository.findAllByName(name);
                if (personList.size() > 1) {
                    tempMap.put(code, name);
                    break;
                }
                //更新编号
                Person newPerson = personList.get(0);
                newPerson.setCode(code);
                Person savePerson = personRepository.save(newPerson);
                log.info("=======人员名称=" + savePerson.getName() + "=======人员编号=" + savePerson.getCode() + "=====================================");
            }
            //打印名称重复的
            log.info("=======================总数:" + tempMap.size() + "=================");
            tempMap.keySet().stream().forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(null);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值