excel文件 —— array(导入)

/*导入*/
	let nodeData = ref()
	// 文件上传
	const uploadFile = function(file) {
		var groups = []
		// excel文件 —— array
		const reader = new FileReader();
		reader.readAsArrayBuffer(file.raw);
		reader.onload = function(e) {
			const data = new Uint8Array(e.target.result);
			const workbook = XLSX.read(data, {
				type: 'array'
			});
			const firstSheetName = workbook.SheetNames[0];
			const worksheet = workbook.Sheets[firstSheetName];
			const jsonData = XLSX.utils.sheet_to_json(worksheet);
			//去重
			let result = jsonData.filter((item, index, self) => {
				return self.findIndex(t => t.group === item.group) === index;
			});
			result.forEach(item => {
				groups.push({
					group: item.group,
					interval: item.interval,
					tags: []
				})
			})
			groups.forEach(item => {
				jsonData.forEach(items => {
					if(items.attribute == 'Read') {
						items.attribute = 1
					} else if(items.attribute == 'Write') {
						items.attribute = 2
					} else if(items.attribute == 'Read Write') {
						items.attribute = 3
					} else if(items.attribute == 'Subscribe') {
						items.attribute = 4
					} else if(items.attribute == 'Read Subscribe') {
						items.attribute = 5
					} else if(items.attribute == 'Write Subscribe') {
						items.attribute = 6
					} else if(items.attribute == 'Read Write Subscribe') {
						items.attribute = 7
					}
					if(items.type == 'INT16') {
						items.type = 3
					} else if(items.type == 'UINT16') {
						items.type = 4
					} else if(items.type == 'INT32') {
						items.type = 5
					} else if(items.type == 'UINT32') {
						items.type = 6
					} else if(items.type == 'INT64') {
						items.type = 7
					} else if(items.type == 'UINT64') {
						items.type = 8
					} else if(items.type == 'FLOAT') {
						items.type = 9
					} else if(items.type == 'DOUBLE') {
						items.type = 10
					} else if(items.type == 'BIT') {
						items.type = 11
					} else if(items.type == 'STRING') {
						items.type = 13
					} else if(items.type == 'BYTES') {
						items.type = 14
					}
					if(item.group == items.group) {
						item.tags.push(items)
					}
				})
			})
			importClick(groups)
		};
	}
	const importClick = function(groups) {
		importGtags({
			groups: groups,
			node: router.currentRoute.value.query.node,
		}).then(res => {
			if(res.status == 200) {
				ElMessage({
					type: 'success',
					message: '导入成功',
				})
				getListFun()
			} else {
				ElMessage({
					type: 'error',
					message: res.data.message,
				})
			}
		}).catch(err => {
			console.log(err);
		})
	}

  注意:

    记得安包XLSX 

    引用 import * as XLSX from 'xlsx'

posted @ 2024-07-31 16:14  nnc  阅读(24)  评论(0)    收藏  举报