当遇到这么一份漂亮的execl时 心情都是舒畅的,是不是心里想着一定要把这个表格弄成树形数据 才觉得过瘾(末尾附送java 源码)
先给大家伙看一下这份漂亮的execl 这份数据出自于某位大师之手
很美观对吧, 现在开始把这份数据转换成树形结构
在人类眼中 这份数据的父子关系非常明确 但是拿到这样的数据要怎么才能教会计算机将其变成一个树形结构的数据呢
下面我把数据的样子转变一下,这样就显得很清晰明了
有了这样的图做出一份树形结构分分钟的事情可以做出一份树形列表呀
做出这样的结构需首先要考虑的是节点,每一颗树都有1-N个节点,每个节点下也可以有多个小节点,就像树一样
把刚刚的表格文件画成树,每个树都有自己的属性
节点深度 可以理解为层级 树有多深层级就有多高
那么怎么将其转化成代码呢 这个节点深度非常重要,可以说是一个家族开枝散叶的基石,给每个节点定义好属性之后,请看下图
如何判断节点信息从而进行存储
接下来开始上手怎么用这样的数据创建一份树形数据
偷个懒 直接看源码吧
链接:https://pan.baidu.com/s/1BLpqG6VZzF-kFfNWj0woMQ 提取码:96zw --来自百度网盘超级会员V5的分享 没错我就是那个超级V5
源码
import com.ruoyi.common.utils.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import java.io.*;
import java.util.*;
public class Execl01Demo {
@Test
public void testExcel() {
String path = "C:\\Users\\16146\\Desktop\\数据.xlsx";
// String path = "D:\\temp\\temp\\test.xlsx";
File file = new File(path);
InputStream is = null;
Workbook workbook = null;
List<Demo01> listss = new ArrayList<>();
try {
is = new FileInputStream(file);
if (path.endsWith(".xls")) {
workbook = new HSSFWorkbook(is);
} else if (path.endsWith(".xlsx")) {
workbook = new XSSFWorkbook(is);
}
if (workbook != null) {
//获取sheet
int sheetCount = workbook.getNumberOfSheets();
//节点
Map<String, Object> nodeMap = new HashMap<>();
if (sheetCount > 0) {
// 文本内容
int id = 0;
for (int i = 0; i < sheetCount; i++) {
Sheet sheet = workbook.getSheetAt(i);
for (int rownum = sheet.getFirstRowNum(); rownum <= sheet.getLastRowNum(); rownum++) {
Row row = sheet.getRow(rownum);
if (row == null || row.getFirstCellNum() < 0) {
break;
}
for (int columnnum = row.getFirstCellNum(); columnnum <= row
.getLastCellNum(); columnnum++) {
String cellValue = getCellValue(row.getCell(columnnum));
Demo01 demo01 = new Demo01();
//第一级
if (StringUtils.isNotEmpty(cellValue)) {
if(columnnum==0) {
nodeMap.clear();
}
demo01.setName(cellValue);
demo01.setNode(columnnum);
demo01.setNodeId(id + "");
String ancestors = "";
String s = String.valueOf(nodeMap.get(String.valueOf(columnnum)));
if (StringUtils.isNotEmpty(s) && s != "null") {
demo01.setParent(String.valueOf(nodeMap.get(String.valueOf(columnnum - 1))));
for (int j = 0; j < columnnum; j++) {
String s1 = String.valueOf(nodeMap.get(String.valueOf(j)));
if (s1!="null") {
ancestors+= s1+"、";
}
}
demo01.setAncestors(ancestors+id+"、");
nodeMap.put(String.valueOf(columnnum), id);
}
else {
nodeMap.put(String.valueOf(columnnum), id);
demo01.setParent(String.valueOf(nodeMap.get(String.valueOf(columnnum - 1))));
for (int j = 0; j < nodeMap.size(); j++) {
String s1 = String.valueOf(nodeMap.get(String.valueOf(j - 1)));
if (s1!="null") {
ancestors+= s1+"、";
}
}
demo01.setAncestors(ancestors);
}
id++;
listss.add(demo01);
}
}
}
}
}
System.out.println();
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
try {
if (workbook != null) {
workbook.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
}
}
}
/**
* 获取单元格内容
*
* @param cell
* @return
*/
private String getCellValue(Cell cell) {
if (cell == null) {
return "";
}
// 都按文本格式读取
cell.setCellType(CellType.STRING);
return cell.getStringCellValue();
}
}