龙空技术网

node读取excel并转化存入mysql

何小玍 204

前言:

如今我们对“mysql转换成excel”大约比较关心,小伙伴们都想要学习一些“mysql转换成excel”的相关资讯。那么小编也在网上收集了一些有关“mysql转换成excel””的相关资讯,希望咱们能喜欢,兄弟们快快来了解一下吧!

vue3导入excel并转化成数组

示范excel的demo

先导入node-xlsx

npm install node-xlsxconst xlsx = require("node-xlsx")
读取excel的数据并进行数据转化
router.get("/commit", async (ctx, next) => {	const m = xlsx.parse('./commit/2.xlsx')	let table = new Array()	m.forEach(item => {		for(let i = 1; i < item['data'].length; i++) {			let arr = new Array()			for(let o = 9; o < item['data'][i].length; o++) {				const t = item['data'][i][o] ? item['data'][i][o] + '--' : item['data'][i][o]				arr.push(t)			}			table[i - 1] = {				"dishes": item['data'][i][0],				"introduce": item['data'][i][1],				"ingredients": item['data'][i][2],				"label": item['data'][i][3],				"tips": item['data'][i][4],				"hor": item['data'][i][5],				"way": item['data'][i][6],				"time": item['data'][i][7],				"flavor": item['data'][i][8],				"steps": arr			}		}	})	ctx.body = util.success(null, "新增成功")})

目前这部分steps存入数据库,是用'--,'这种符号去隔开,到时候查出来,组成数组,供前端使用

数据转化并存入mysql

router.get("/commit", async (ctx, next) => {	const m = xlsx.parse('./commit/2.xlsx')	m.forEach(item => {		for(let i = 1; i < item['data'].length; i++) {			let arr = new Array()			for(let o = 9; o < item['data'][i].length; o++) {				const t = item['data'][i][o] ? item['data'][i][o] + '--' : item['data'][i][o]				arr.push(t)			}			// 写入mysql			const sql = `				insert into daemon_food					(dishes, introduce, ingredients, label, tips, hor, way, time, flavor, steps)				values					("${ item['data'][i][0] ?? null }",					 "${ item['data'][i][1] ?? null }",					 "${ item['data'][i][2] ?? null }",					 "${ item['data'][i][3] ?? null }",					 "${ item['data'][i][4] ?? null }",					 "${ item['data'][i][5] ?? null }",					 "${ item['data'][i][6] ?? null }",					 "${ item['data'][i][7] ?? null }",					 "${ item['data'][i][8] ?? null }",					 "${ arr ?? null }")			`			query(sql)		}	})	ctx.body = util.success("", "新增成功")})

本次数据一共14万多的数据,循环导入,跑了差不多3分钟,自己研究的写法,如果有好的办法,烦请大佬教教我。

最后

公众号:小何成长,佛系更文,都是自己曾经踩过的坑或者是学到的东西

有兴趣的小伙伴欢迎关注我哦,我是:何小玍。大家一起进步鸭

标签: #mysql转换成excel