const fs = require('fs')
//读xlsx文件
const xlsx = require('node-xlsx')
let sheets = xlsx.parse('./1.xlsx')
sheets.forEach(item => {
let sheetsArr = []
let arr = item.data[0]
item.data.forEach((e, i) => {
if (i != 0) {
let obj = {}
arr.forEach((titleItem, titleIndex) => {
obj[titleItem] = e[titleIndex]
})
sheetsArr.push(obj)
}
})
fs.writeFile(`${item.name}.json`, JSON.stringify(sheetsArr), res => {
console.log('写入成功')
})
})
node-xlsx模块用来解析/构建excel工作表
npm install node-xlsx --save
const xlsx = require('node-xlsx')
let sheets = xlsx.parse('./1.xlsx')
console.log(sheets)
(1). 数据存放数组
sheets.forEach(item => {
let sheetsArr = []
let arr = item.data[0]
item.data.forEach((e, i) => {
if (i != 0) {
let obj = {}
arr.forEach((titleItem, titleIndex) => {
obj[titleItem] = e[titleIndex]
})
sheetsArr.push(obj)
}
})
console.log(sheetsArr)
})
sheets.forEach(item => {
let sheetsArr = []
let arr = item.data[0]
item.data.forEach((e, i) => {
if (i != 0) {
let obj = {}
arr.forEach((titleItem, titleIndex) => {
obj[titleItem] = e[titleIndex]
})
sheetsArr.push(obj)
}
})
fs.writeFile(`${item.name}.json`, JSON.stringify(sheetsArr), res => {
console.log('写入成功')
})
})