app.js:
var FileUpload = require('express-fileupload') app.use(FileUpload());
service.js:
npm install -S xlsx
var XLSX= require('xlsx');
let excelData = []; let paramsArr = []; let nowTime = parseInt(moment().format('X')); let username = res.locals.username; let excelFile = req.files.file.data; // const workbook = XLSX.readFile('./public/files/xxx.xlsx'); const workbook = XLSX.read(excelFile); const sheetNames = workbook.SheetNames; for (var sheet in workbook.Sheets) { if (workbook.Sheets.hasOwnProperty(sheet)) { fromTo = workbook.Sheets[sheet]['!ref']; //解析excel文件得到数据 excelData = excelData.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet])); } } let sheetsData = Collect(excelData).map(item => { return { a : item['SN'], d: item['x'x'x'], b : item['PN'], c : item['xxx'], e : item['xxx'], g : '', Ctime : nowTime, Uptime : nowTime, u : username } }).all();
参考网址:
https://www.npmjs.com/package/express-fileupload
https://github.com/SheetJS/js-xlsx
https://scarletsky.github.io/2016/01/30/nodejs-process-excel/