Node.js 读取 excel

11,278 阅读1分钟

使用node-xlsx模块读取excel内容

使用比较简单,2行代码就可以解析出excel内容,可以根据具体需求转换读取到的excel数据.

安装 node-xlsx 模块
npm install node-xlsx
node.js 代码
// 引入 node-xlsx 模块
const xlsx = require('node-xlsx')

// excel文件类径
const excelFilePath = './test.xlsx'

//解析excel, 获取到所有sheets
const sheets = xlsx.parse(excelFilePath);

// 查看页面数
console.log(sheets.length);

// 打印页面信息..
const sheet = sheets[0];
console.log(sheet);

// 打印页面数据
console.log(sheet.data);

// 输出每行内容
sheet.data.forEach(row => {
    console.log(row);
    // 数组格式, 根据不同的索引取数据
})