【JAVA】读取excel导入数据库,形成树状结构

最近需要导入一个excel表格,存到数据库并以树状结构读取出来
下面两张图片是需要导入的excel
在这里插入图片描述


    @Transactional(rollbackFor = Exception.class)
    public String import(MultipartFile file, Integer projectId) throws Exception {
        //poi解析excel
        InputStream inputStream = file.getInputStream();
        // 读取工作薄
        Workbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);
        //定义一个cellId,此为每一次循环前一列的id
        long cellId = 0;
        //根据项目id查询项目名称
        ProjectManager one = projectManagerService.lambdaQuery().select(ProjectManager::getProjectName).eq(ProjectManager::getId, projectId).one();
        int rows = sheet.getPhysicalNumberOfRows();
        if (rows > 1) {
            //按照行进行循环,读取当前行的列
            for (int k = 1; k < rows; k++) {
                // 读取行
                Row row = sheet.getRow(k);
                //查询当前行有多少列
                int physical = sheet.getRow(k).getPhysicalNumberOfCells();
                
                if (row != null && physical > 3) {
                    //获取第一列的单元格
                    Cell cell1 = row.getCell(0);
                    //判断单元格是否为空
                    if (!(cell1 == null || "".equals(cell1.toString().trim()))) {
                        //根据单元格的数据查询数据库是否存在记录
                        IndexInfo one1 = indexInfoService.lambdaQuery().select(IndexInfo::getIndexPname, IndexInfo::getId)
                                .eq(IndexInfo::getIndexPname, cell1.getStringCellValue()).eq(IndexInfo::getProjectId, projectId).one();
                        //数据库不存在,添加记录
                        if (one1 == null) {
                            IndexInfo indexInfo = new IndexInfo();
                            indexInfo.setIndexPid(0L);
                            indexInfo.setIndexPname(cell1.getStringCellValue());
                            indexInfo.setProjectName(one.getProjectName());
                            indexInfo.setProjectId(projectId);
                            indexInfo.setIndexClass(2);
                            boolean save = indexInfoService.save(indexInfo);
                            if (save) {
                                //添加成功,将添加的id作为父id
                                cellId = indexInfo.getId();
                            }
                            //数据库存在记录,将这条记录的id作为父id
                        } else {
                            cellId = one1.getId();
                        }
                    }
                }
                for (int j = 1; j < physical - 2; j++) {
                    //取单元格
                    Cell cell = row.getCell(j);
                    //判断单元格是否为空
                    if (!(cell == null || "".equals(cell.toString().trim()))) {
                        //查询数据库有无此记录
                        IndexInfo one1 = indexInfoService.lambdaQuery().select(IndexInfo::getIndexPname, IndexInfo::getId)
                                .eq(IndexInfo::getIndexPname, cell.getStringCellValue()).eq(IndexInfo::getProjectId, projectId).one();
                        if (one1 == null) {
                        //这里保存自己需要的信息
                            IndexInfo indexInfo = new IndexInfo();
                            indexInfo.setIndexPid(cellId);
                            indexInfo.setIndexPname(cell.getStringCellValue());
                            indexInfo.setProjectName(one.getProjectName());
                            indexInfo.setProjectId(projectId);
                            indexInfo.setIndexClass(2);
                            //最后两列为描述和分数
                            if (j == physical - 3) {
                                int score = 0;
                                String description = null;
                                //循环最后两列单元格
                                for (int i = 0; i < 2; i++) {
                                    //取单元格的值
                                    Cell cell1 = row.getCell(j + i + 1);
                                    //判断单元格是否为空
                                    if (!(cell == null || "".equals(cell.toString().trim()))) {
                                        try {
                                            //因为分数是int类型,如果报错就是取了分数单元格的值
                                            description = cell1.getStringCellValue();
                                        } catch (Exception e) {
                                            score = (int) cell1.getNumericCellValue();
                                        }
                                        indexInfo.setIndexScore(score);
                                        indexInfo.setIndexDescription(description);
                                    }
                                }
                            }
                            boolean save = indexInfoService.save(indexInfo);
                            if (save) {
                                cellId = indexInfo.getId();
                            }
                        } else {
                            cellId = one1.getId();
                        }
                    }
                }
            }
            return "文件导入成功";
        } else {
            return "导入失败";
        }
    }

还有一个这种的表格,多读一列就可以了
在这里插入图片描述

@Transactional(rollbackFor = Exception.class)
    public String import(MultipartFile file, Integer projectId) throws Exception {
        //poi解析excel
        InputStream inputStream = file.getInputStream();
        // 读取工作薄
        Workbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);
        //定义一个cellId,此为每一次循环前一列的id
        long cellId = 0;
        //根据项目id查询项目名称
        ProjectManager one = projectManagerService.lambdaQuery().select(ProjectManager::getProjectName).eq(ProjectManager::getId, projectId).one();
        int rows = sheet.getPhysicalNumberOfRows();
        if (rows > 1) {
            //按照行进行循环,读取当前行的列
            for (int k = 1; k < rows; k++) {
                // 读取行
                Row row = sheet.getRow(k);
                //查询当前行有多少列
                int physical = sheet.getRow(k).getPhysicalNumberOfCells();
             
                if (row != null && physical > 3) {
                    //获取第一列的单元格
                    Cell cell = row.getCell(0);
                    //判断单元格是否为空
                    if (!(cell == null || "".equals(cell.toString().trim()))) {
                        //根据单元格的数据查询数据库是否存在记录
                        IndexInfo one1 = indexInfoService.lambdaQuery().select(IndexInfo::getIndexPname, IndexInfo::getId)
                                .eq(IndexInfo::getIndexPname, cell.getStringCellValue()).eq(IndexInfo::getProjectId, projectId).one();
                        //数据库不存在,添加记录
                        if (one1 == null) {
                            IndexInfo indexInfo = new IndexInfo();
                            indexInfo.setIndexPid(0L);
                            indexInfo.setIndexPname(cell.getStringCellValue());
                            indexInfo.setProjectName(one.getProjectName());
                            indexInfo.setProjectId(projectId);
                            indexInfo.setIndexClass(2);
                            indexInfo.setIndexWeight(row.getCell(1).getNumericCellValue());
                            boolean save = indexInfoService.save(indexInfo);
                            if (save) {
                                //添加成功,将添加的id作为父id
                                cellId = indexInfo.getId();
                            }
                            //数据库存在记录,将这条记录的id作为父id
                        } else {
                            cellId = one1.getId();
                        }
                    }
                }
                for (int i = 2; i < physical-1; i += 2) {
                    //取单元格
                    Cell cell = row.getCell(i);
                    //判断单元格是否为空
                    if (!(cell == null || "".equals(cell.toString().trim()))) {
                        //查询数据库有无此记录
                        IndexInfo one1 = indexInfoService.lambdaQuery().select(IndexInfo::getIndexPname, IndexInfo::getId)
                                .eq(IndexInfo::getIndexPname, cell.getStringCellValue()).eq(IndexInfo::getProjectId, projectId).one();
                        if (one1 == null) {
                            IndexInfo indexInfo = new IndexInfo();
                            indexInfo.setIndexPid(cellId);
                            indexInfo.setProjectName(one.getProjectName());
                            indexInfo.setProjectId(projectId);
                            indexInfo.setIndexPname(cell.getStringCellValue());
                            indexInfo.setIndexClass(2);
                            indexInfo.setIndexWeight(row.getCell(i +1).getNumericCellValue());
                            //最后两列为描述和分数
                            if (i == physical - 3) {
                                //循环最后两列单元格
                                indexInfo.setIndexDescription(row.getCell(i +2).getStringCellValue());
                            }
                            boolean save = indexInfoService.save(indexInfo);
                            if (save) {
                                cellId = indexInfo.getId();
                            }
                        } else {
                            cellId = one1.getId();
                        }
                    }
                }
            }
            return "导入成功";
        } else
            return "导入失败";

    }

读取完成保存到数据库后,形成树状结构具体步骤可以看我上一篇帖子

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值