初始化package文件
npm init
安装node-xlsx
npm i node-xlsx --save
引入node-xlsx
用于excel解析,fs 获取生成 txt 文件
const xlsx = require('node-xlsx');
const fs = require('fs')
excel解析及生成excel和txt文件
const xlsx = require('node-xlsx');
const fs = require('fs')
let sheets = xlsx.parse('./input.xls'); //获取到所有sheets
let excel = []
// txt创建
let txt = fs.createWriteStream('./output.txt', {
encoding: 'utf8'
});
sheets.forEach(function(sheet) {
for (let rowId in sheet['data']) {
let row = sheet['data'][rowId];
excel.push(row)
txt.write('\r\n')
txt.write(row.join(' '))
}
});
txt.end();
// 写入excel文件中
let buffer = xlsx.build([{ name: 'name', data: excel }]);
fs.writeFileSync('./output.xls', buffer, 'binary')