I received an import task today, which requires saving excel data into the database. Different from ordinary import, the imported data is a tree structure, as shown below:
By observing the hierarchical columns in the data, we found that the table data consists of 2 trees, namely 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th and 12th. ,13,14,15,16,17,18, they use 0 as the root node of the tree, 1 as the child node of 0, and 2 as the child node of adjacent 1. From this, the structure of the first tree is:
Create vo to receive parsed data. Here, we only care about hierarchical attributes
@Excel(name = "层级") private String hierarchy; @Excel(name = "物料编码") private String materialCode; @Excel(name = "物料名称") private String materialName; @Excel(name = "基础数量") private BigDecimal materialNum; @Excel(name = "使用数量") private BigDecimal useAmount; @Excel(name = "BOM版本") private String version; @Excel(name = "默认BOM") private String isDefaults;
Split the data source into several tree data sets
The code is as follows (example):
/** * 将集合对象按指定元素分割存储 * * @param materialVos 原始集合 * @param s 分割元素(这里是当集合对象层级为0时则分割,也就是树的根节点为0) * @return 每棵树的结果集 */ private List<List<MatMaterialBomImportVo>> subsection(List<MatMaterialBomImportVo> materialVos, String s) { List<List<MatMaterialBomImportVo>> segmentedData = new ArrayList<>(); if (materialVos != null) { //获取指定元素的数量,判断出最终将拆分为多少段 List<MatMaterialBomImportVo> collect = materialVos.stream().filter(bom -> s.equals(bom.getHierarchy())).collect(Collectors.toList()); int count = 0; for (int i = 0; i < collect.size(); i++) { List<MatMaterialBomImportVo> bomImportVo = new ArrayList<>(); boolean num = false; //遍历数据源 for (; count < materialVos.size(); count++) { //第一个必然为树的根节点,直接获取并跳过 if (count == 0) { bomImportVo.add(materialVos.get(count)); continue; } //当数据源第n个等于根节点并且已经成功添加过数据时判断为一段数据的结束,跳出循环, if (s.equals(materialVos.get(count).getHierarchy()) && num) { break; } bomImportVo.add(materialVos.get(count)); num = true; } segmentedData.add(bomImportVo); } } return segmentedData; }
The code is as follows (example):
for (List<MatMaterialBomImportVo> segmentedDatum : subsection(materialVos, "0")) { //设置id以及父id int i = 0; for (MatMaterialBomImportVo vo : segmentedDatum) { BeanTrim.beanAttributeValueTrim(vo); vo.setPrimaryKey(i); getParentId(vo, segmentedDatum); i++; } } /** * 设置父id * * @param vo * @param segmentedDatum */ private void getParentId(MatMaterialBomImportVo vo, List<MatMaterialBomImportVo> segmentedDatum) { for (int j = vo.getPrimaryKey(); j >= 0; j--) { if (Integer.parseInt(segmentedDatum.get(j).getHierarchy()) == Integer.parseInt(vo.getHierarchy()) - 1) { vo.setForeignKey(segmentedDatum.get(j).getPrimaryKey()); break; } if (j == 0) { vo.setForeignKey(-1); } } }
Instructions: After splitting into several trees, set the virtual id of each piece of data as its own index ,The IDs of each tree are isolated from each other.
According to the rules of table data, it can be concluded that child nodes can only exist below their own nodes and above the next same node. According to this rule, set the parent ID of each node
The code is as follows (example):
/** * 递归遍历为树形结构 * * @param vo 当前处理的元素 * @param segmentedDatum 每棵树的数据集 */ private void treeData(MatMaterialBomImportVo vo, List<MatMaterialBomImportVo> segmentedDatum) { for (int i = vo.getPrimaryKey(); i < segmentedDatum.size(); i++) { if (i + 1 == segmentedDatum.size()) { if (vo.getForeignKey() == null) { getParentId(vo, segmentedDatum); } break; } int v = Integer.parseInt(vo.getHierarchy()); int vs = Integer.parseInt(segmentedDatum.get(i + 1).getHierarchy()); if (vs == v + 1) { if (v > 1) { vo.setForeignKey(segmentedDatum.get(i).getPrimaryKey()); for (int j = vo.getPrimaryKey(); j > 0; j--) { if (Integer.parseInt(segmentedDatum.get(j).getHierarchy()) == Integer.parseInt(vo.getHierarchy()) - 1) { vo.setForeignKey(segmentedDatum.get(j).getPrimaryKey()); } } } vo.getImportVoList().add(segmentedDatum.get(i + 1)); } if (vs <= v) { if (vo.getForeignKey() == null) { for (int j = vo.getPrimaryKey(); j > 0; j--) { if (Integer.parseInt(segmentedDatum.get(j).getHierarchy()) == Integer.parseInt(vo.getHierarchy()) - 1) { vo.setForeignKey(segmentedDatum.get(j).getPrimaryKey()); break; } } } break; } } if (vo.getImportVoList() != null && vo.getImportVoList().size() > 0) { for (MatMaterialBomImportVo matMaterialBomImportVo : vo.getImportVoList()) { treeData(matMaterialBomImportVo, segmentedDatum); } } }
Explanation: The vo I passed in here does not have the id and parent id set, only the data source The tree splitting process was done. Due to business requirements, this set of recursive methods was not used to assemble the tree later, so the recursive code may have some errors, for reference only
The above is the detailed content of How to convert Excel data into tree structure in Java?. For more information, please follow other related articles on the PHP Chinese website!