二维数组下载为excel(导出)

/*导出*/
	const s2ab = function(s) {
		const buf = new ArrayBuffer(s.length);
		const view = new Uint8Array(buf);
		for(let i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xFF;
		return buf;
	}
	const exportClick = async function() {
		//多个组数据处理
		let idList = []
		multipleSelection.value.forEach(item => {
			idList.push({
				group: item.name,
				interval: item.interval
			})
		})
		if(multipleSelection.value.length > 0) {
			var k = 0
			var jsonArr = []
			for(var g = 0; g < idList.length; g++) {
				k++
				await tags({
					group: idList[g].group,
					node: router.currentRoute.value.query.node
				}).then(res => {
					if(res.status == 200) {
						for(var i = 0; i < res.data.tags.length; i++) {
							res.data.tags[i].group = idList[g].group
							res.data.tags[i].interval = idList[g].interval
							if(res.data.tags[i].attribute == 1) {
								res.data.tags[i].attribute = 'Read'
							} else if(res.data.tags[i].attribute == 2) {
								res.data.tags[i].attribute = 'Write'
							} else if(res.data.tags[i].attribute == 3) {
								res.data.tags[i].attribute = 'Read Write'
							} else if(res.data.tags[i].attribute == 4) {
								res.data.tags[i].attribute = 'Subscribe'
							} else if(res.data.tags[i].attribute == 5) {
								res.data.tags[i].attribute = 'Read Subscribe'
							} else if(res.data.tags[i].attribute == 6) {
								res.data.tags[i].attribute = 'Write Subscribe'
							} else if(res.data.tags[i].attribute == 7) {
								res.data.tags[i].attribute = 'Read Write Subscribe'
							}
							if(res.data.tags[i].type == 3) {
								res.data.tags[i].type = 'INT16'
							} else if(res.data.tags[i].type == 4) {
								res.data.tags[i].type = 'UINT16'
							} else if(res.data.tags[i].type == 5) {
								res.data.tags[i].type = 'INT32'
							} else if(res.data.tags[i].type == 6) {
								res.data.tags[i].type = 'UINT32'
							} else if(res.data.tags[i].type == 7) {
								res.data.tags[i].type = 'INT64'
							} else if(res.data.tags[i].type == 8) {
								res.data.tags[i].type = 'UINT64'
							} else if(res.data.tags[i].type == 9) {
								res.data.tags[i].type = 'FLOAT'
							} else if(res.data.tags[i].type == 10) {
								res.data.tags[i].type = 'DOUBLE'
							} else if(res.data.tags[i].type == 11) {
								res.data.tags[i].type = 'BIT'
							} else if(res.data.tags[i].type == 13) {
								res.data.tags[i].type = 'STRING'
							} else if(res.data.tags[i].type == 14) {
								res.data.tags[i].type = 'BYTES'
							}
							jsonArr.push(res.data.tags[i])
						}
						if(k == idList.length) {
							const data = jsonArr.map(obj => [obj.group, obj.interval, obj.name, obj.address, obj.attribute, obj.type, obj.description, obj.decimal, obj.precision]);
							data.unshift(['group', 'interval', 'name', 'address', 'attribute', 'type', 'description', 'decimal', 'precision', ])
							/*二维数组下载为excel*/
							const worksheet = XLSX.utils.aoa_to_sheet(data);
							const workbook = XLSX.utils.book_new();
							XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
							const element = document.createElement('a');
							const blob = new Blob([s2ab(XLSX.write(workbook, {
								bookType: 'xlsx',
								type: 'binary'
							}))], {
								type: "application/octet-stream"
							});
							element.href = URL.createObjectURL(blob);
							element.download = 'tags.xlsx';
							element.click(); // 模拟点击下载
							ElMessage({
								type: 'success',
								message: '导出成功',
							})
							getListFun()
						}

					} else {
						ElMessage({
							type: 'error',
							message: res.data.message,
						})
					}
				}).catch(err => {
					console.log(err);
				})
			}

		} else {
			ElMessage({
				type: 'info',
				message: '请至少选择一条导出的数据!'
			})
		}
	}

  

  注意:

    记得安包XLSX 

    引用 import * as XLSX from 'xlsx'

posted @ 2024-07-31 16:12  nnc  阅读(6)  评论(0编辑  收藏  举报