<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta charset="utf-8" /> <title>excel to array-tree-sql</title> </head> <body> <textarea class="nr-txt-excel" placeholder="粘贴 excel"></textarea> <button class="nr-btn">To (Open console || F12)</button> </body> </html>
textarea { height: 5em; width: 98vw; margin: 1em 0; }
let nrExcel = { toArray: (excel) => { let keys = []; let result = []; excel.split('\n').forEach(row => { if (row.trim() != "") { let cols = row.split('\t'); if (keys.length == 0) { keys = cols.map(x => x.trim()); } else { let row = {}; for (let index = 0; index < cols.length; index++) { let col = cols[index].trim(); if (col == "") { col = null; } else if (parseFloat(col).toString() == col) { col = parseFloat(col); } row[keys[index]] = col; } result.push(row); } } }); return result; }, toTree: (arr, startId, startPid) => { startId = startId || 1; startPid = startPid || 0; let result = []; for (let i = 0; i < arr.length; i++) { let item = arr[i]; let values = Object.values(item); //非全部为 null if (values.filter(x => x == null).length != values.length) { let col1 = item["集群名称"]; let col2 = item["大类代码"] let col3 = item["大类名称"] let col4 = item["中类代码"] let col5 = item["中类名称"] let col6 = item["小类代码"] let col7 = item["小类名称"] if (col1 != null) { startId++; result.push({ id: startId, text: col1, code: null, pid: startPid, level: "集群" }); } if (col3 != null) { let pid = nrExcel.findPid(result, '集群'); startId++ result.push({ id: startId, text: col3, code: col2, pid, level: "大类" }); } if (col5 != null) { let pid = nrExcel.findPid(result, '大类'); startId++ result.push({ id: startId, text: col5, code: col4, pid, level: "中类" }); } if (col7 != null) { let pid = nrExcel.findPid(result, '中类'); startId++ result.push({ id: startId, text: col7, code: col6, pid, level: "小类" }); } } } return result; }, findPid: (result, lastLevel) => { let pid = 0; for (let i = result.length - 1; i >= 0; i--) { let item = result[i]; if (item.level == lastLevel) { pid = item.id; break; } } return pid; }, buildSql: (tree) => { let resutl = tree.map(item => { let code = item.code == null ? "NULL" : `'${item.code}'`; return `INSERT INTO PT_INDUSTRY_THEME VALUES (${item.id}, '${item.text.replaceAll("'", "''")}', '0', ${item.id}, TIMESTAMP '2023-07-12 17:30:00', NULL, ${item.pid}, ${code})` }) return resutl; } } let domTxtExcel = document.querySelector('.nr-txt-excel'); let domTxtResult = document.querySelector('.nr-txt-result'); domTxtExcel.value = `集群名称 大类代码 大类名称 中类代码 中类名称 小类代码 小类名称 智能网联新能源汽车产业集群 36 汽车制造业 361 汽车整车制造 3611 汽柴油车整车制造 3612 新能源车整车制造 362 汽车用发动机制造 3620 汽车用发动机制造 363 改装汽车制造 3630 改装汽车制造 364 低速汽车制造 3640 低速汽车制造 365 电车制造 3650 电车制造 366 汽车车身、挂车制造 3660 汽车车身、挂车制造 367 汽车零部件及配件制造 3670 汽车零部件及配件制造 `; document.querySelector('.nr-btn').addEventListener('click', () => { // 转成 json 数组 let arr = nrExcel.toArray(domTxtExcel.value); console.debug(arr); // 转成 tree let tree = nrExcel.toTree(arr, 1, 0); console.debug(tree); // 构建 sql let sql = nrExcel.buildSql(tree); console.debug(sql.join(';\r\n')) })