行政区划代码
2022版本三沙市下面二个区没有编码需要自行删除
注:1.台湾省、香港特别行政区和澳门特别行政区暂缺地市和区县信息
<template>
<div>
<div style="color:red"><input type="file" @change="readFile" accept=".xlsx, .xls">
</div>
</div>
</template>
<script>
import XLSX from 'xlsx'
export default {
name: 'VmsWebTest',
data() {
return {
};
},
mounted() {
},
methods: {
readFile(e) {//上传文件后读取excel文件内容
let file = e.target.files[0];
const types = ["xlsx", "xls"];
const arr = file.name.split(".");
//判断文件是否为excel文件
if (!types.find(item => item === arr[arr.length - 1])) {
alert('请选择正确的文件类型');
return;
}
let reader = new FileReader();
//启动函数
reader.readAsBinaryString(file);
reader.onload = e => {
console.log(e);
//workbook存放excel的所有基本信息
let workbook = XLSX.read(e.target.result, { type: "binary", cellDates: true });
//定义sheetList中存放excel表格的sheet表,就是最下方的tab
let sheetList = workbook.SheetNames;
//读取文件内容,(第一个sheet里的内容)
let json = XLSX.utils.sheet_to_json(workbook.Sheets[sheetList[0]],);
console.log(json);
this.jsontree(json)
}
},
jsontree(arr) {
arr.forEach(item => {
item.id += ''
if (item.id.indexOf('0000') != -1) {
item.parentId = 0
} else if (item.id.indexOf('00') != -1) {
item.parentId = item.id.slice(0, 2) + '0000'
} else {
if (item.id.slice(0, 4)=='1101' || item.id.slice(0, 4)=='1201') {
item.parentId = item.id.slice(0, 2) + '0000'
} else {
item.parentId = item.id.slice(0, 4) + '00'
}
}
});
let data = this.handleTree(arr, 'id')
console.log(data);
},
handleTree(data, id, parentId, children, rootId) {
id = id || 'id'
parentId = parentId || 'parentId'
children = children || 'children'
rootId = rootId || Math.min.apply(Math, data.map(item => { return item[parentId] })) || 0
//对源数据深度克隆
const cloneData = JSON.parse(JSON.stringify(data))
//循环所有项
const treeData = cloneData.filter(father => {
let branchArr = cloneData.filter(child => {
//返回每一项的子级数组
return father[id] === child[parentId]
});
branchArr.length > 0 ? father.children = branchArr : '';
//返回第一层
return father[parentId] === rootId;
});
return treeData != '' ? treeData : data;
}
},
};
</script>
<style lang="scss" scoped></style>