安装组件
cnpm i xlsx --save
config.default.js 配置
config.multipart = {
fileSize: '100mb',
mode: 'file',
whitelist() {
return true;
},
};
注意:config.multipart.whitelist 设置上传文件格式不限制,如果要限制,请移除config.multipart.whitelist 配置项或自行添加文件后缀
controller.js 代码
引入
const xlsx = require('xlsx');
const fs = require('mz/fs');
async importOrg() {
let file;
try {
file = this.ctx.request.files[0];
const list = new Array(0);
const workbook = xlsx.readFile(file.filepath);
for (const sheet in workbook.Sheets) {
if (workbook.Sheets.hasOwnProperty(sheet)) {
// 数据直接转成json格式
list.push(xlsx.utils.sheet_to_json(workbook.Sheets[sheet]));
}
}
this.ctx.body = new Result(templist);
} catch (error) {
this.ctx.logger.error(error);
if (error.classtype === 'BusError') {
this.ctx.body = new Result(null, false, error.message, error.code);
return;
}
this.ctx.body = new Result(null, false, '未知错误', 10000);
} finally {
if (file) {
await fs.unlink(file.filepath);
}
}
}
接下来更精彩,请勿走开(以下是私事,不喜欢别往下看了)
好用的HTTP模拟请求工具
市面上有很多HTTP模拟请求工具,有yapi,有ApiPost,好不好用在此不做评价。这里推荐使用BB-API
,功能全面,免费使用、资源无限制,自动生成接口文档,界面简洁大气。支持局域网部署,打造属于你、公司的HTTP 协议管理工具。
官网地址http://yun-api.com/