日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發(fā)布時間:2024/1/1 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【JAVA】读取excel导入数据库,形成树状结构 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

最近需要導(dǎo)入一個excel表格,存到數(shù)據(jù)庫并以樹狀結(jié)構(gòu)讀取出來
下面兩張圖片是需要導(dǎo)入的excel

@Transactional(rollbackFor = Exception.class)public String import(MultipartFile file, Integer projectId) throws Exception {//poi解析excelInputStream inputStream = file.getInputStream();// 讀取工作薄Workbook workbook = new XSSFWorkbook(inputStream);Sheet sheet = workbook.getSheetAt(0);//定義一個cellId,此為每一次循環(huán)前一列的idlong cellId = 0;//根據(jù)項目id查詢項目名稱ProjectManager one = projectManagerService.lambdaQuery().select(ProjectManager::getProjectName).eq(ProjectManager::getId, projectId).one();int rows = sheet.getPhysicalNumberOfRows();if (rows > 1) {//按照行進行循環(huán),讀取當(dāng)前行的列for (int k = 1; k < rows; k++) {// 讀取行Row row = sheet.getRow(k);//查詢當(dāng)前行有多少列int physical = sheet.getRow(k).getPhysicalNumberOfCells();if (row != null && physical > 3) {//獲取第一列的單元格Cell cell1 = row.getCell(0);//判斷單元格是否為空if (!(cell1 == null || "".equals(cell1.toString().trim()))) {//根據(jù)單元格的數(shù)據(jù)查詢數(shù)據(jù)庫是否存在記錄IndexInfo one1 = indexInfoService.lambdaQuery().select(IndexInfo::getIndexPname, IndexInfo::getId).eq(IndexInfo::getIndexPname, cell1.getStringCellValue()).eq(IndexInfo::getProjectId, projectId).one();//數(shù)據(jù)庫不存在,添加記錄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作為父idcellId = indexInfo.getId();}//數(shù)據(jù)庫存在記錄,將這條記錄的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()))) {//查詢數(shù)據(jù)庫有無此記錄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);//最后兩列為描述和分數(shù)if (j == physical - 3) {int score = 0;String description = null;//循環(huán)最后兩列單元格for (int i = 0; i < 2; i++) {//取單元格的值Cell cell1 = row.getCell(j + i + 1);//判斷單元格是否為空if (!(cell == null || "".equals(cell.toString().trim()))) {try {//因為分數(shù)是int類型,如果報錯就是取了分數(shù)單元格的值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 "文件導(dǎo)入成功";} else {return "導(dǎo)入失敗";}}

還有一個這種的表格,多讀一列就可以了

@Transactional(rollbackFor = Exception.class)public String import(MultipartFile file, Integer projectId) throws Exception {//poi解析excelInputStream inputStream = file.getInputStream();// 讀取工作薄Workbook workbook = new XSSFWorkbook(inputStream);Sheet sheet = workbook.getSheetAt(0);//定義一個cellId,此為每一次循環(huán)前一列的idlong cellId = 0;//根據(jù)項目id查詢項目名稱ProjectManager one = projectManagerService.lambdaQuery().select(ProjectManager::getProjectName).eq(ProjectManager::getId, projectId).one();int rows = sheet.getPhysicalNumberOfRows();if (rows > 1) {//按照行進行循環(huán),讀取當(dāng)前行的列for (int k = 1; k < rows; k++) {// 讀取行Row row = sheet.getRow(k);//查詢當(dāng)前行有多少列int physical = sheet.getRow(k).getPhysicalNumberOfCells();if (row != null && physical > 3) {//獲取第一列的單元格Cell cell = row.getCell(0);//判斷單元格是否為空if (!(cell == null || "".equals(cell.toString().trim()))) {//根據(jù)單元格的數(shù)據(jù)查詢數(shù)據(jù)庫是否存在記錄IndexInfo one1 = indexInfoService.lambdaQuery().select(IndexInfo::getIndexPname, IndexInfo::getId).eq(IndexInfo::getIndexPname, cell.getStringCellValue()).eq(IndexInfo::getProjectId, projectId).one();//數(shù)據(jù)庫不存在,添加記錄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作為父idcellId = indexInfo.getId();}//數(shù)據(jù)庫存在記錄,將這條記錄的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()))) {//查詢數(shù)據(jù)庫有無此記錄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());//最后兩列為描述和分數(shù)if (i == physical - 3) {//循環(huán)最后兩列單元格indexInfo.setIndexDescription(row.getCell(i +2).getStringCellValue());}boolean save = indexInfoService.save(indexInfo);if (save) {cellId = indexInfo.getId();}} else {cellId = one1.getId();}}}}return "導(dǎo)入成功";} elsereturn "導(dǎo)入失敗";}

讀取完成保存到數(shù)據(jù)庫后,形成樹狀結(jié)構(gòu)具體步驟可以看我上一篇帖子

總結(jié)

以上是生活随笔為你收集整理的【JAVA】读取excel导入数据库,形成树状结构的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。