二维数组下载为excel(导出)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | /*导出*/ 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'
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~