纯前端导入excel
import XLSX from 'xlsx';
async operateFile(file) {
// this.formData.userInfos = file.raw;
// this.formData.uploadFile = true;
// this.$refs['form'].validate();
let reader = new FileReader();
//启动函数
try {
reader.readAsBinaryString(file.raw);
} catch (error) {
this.editLoading = false;
}
const self = this;
//onload在文件被读取时自动触发
reader.onload = function(e) {
//workbook存放excel的所有基本信息
let data = e.target.result;
let workbook;
// 区分csv和excel
if (file.raw.type === 'text/csv') {
let str = cptable.utils.decode(65001, data);
workbook = XLSX.read(str, { type: 'string' });
} else {
workbook = XLSX.read(data, {
type: 'binary',
codepage: 65001
});
}
//定义sheetList中存放excel表格的sheet表,就是最下方的tab
let sheetList = workbook.SheetNames;
//存放json数组格式的表格数据
let resultJson = [];
//存放字符串数组格式的表格数据
// sheetList.forEach(function(y) {
let worksheet = workbook.Sheets[sheetList[0]];
const fromTo = worksheet['!ref'];
if (!/^A1:B\d+$/.test(fromTo) || worksheet.B1.v !== '手机号') {
self.editLoading = false;
// self.formData.userInfos = [];
self.$message.error('excel模板不正确');
return;
}
const json = XLSX.utils.sheet_to_json(worksheet);
if (json.length <= 0) {
self.$message.error('没有数据');
self.editLoading = false;
// self.formData.userInfos = [];
return;
} else {
json.forEach(item => {
let ress = { name: '', tel: '' };
if (item['姓名']) {
ress.name = item['姓名'];
}
if (item['手机号']) {
ress.tel = item['手机号'];
}
resultJson.push(ress);
});
}
};
}
纯前端导出csv
node文件流导出excel且显示中文文件名
- 文件流导出excel
重点:Content-Disposition 文件名是中文显示
const nodeExcel = require('node-xlsx');
const urlencode = require('urlencode');
function exportList(ctx, config, listData, excelName) {
let excelConfig = [];
excelConfig.push(config.map(item => {
return item.title
}))
listData.forEach(list => {
excelConfig.push(config.map(item => {
const value = list[item.name];
// 不一定要有value, 因为可能是自由组合的value
return item.format && item.format(value, list) || value;
}))
})
let buffer = nodeExcel.build([{name: excelName, data: excelConfig}]);
ctx.set('Content-Type', 'application/octet-stream');
// ctx.request.headers['user-agent']
let name = urlencode(excelName + '_' + (+new Date()) + '.xlsx', "utf-8");
ctx.set("Content-Disposition", "attachment; filename* = UTF-8''"+name);
// ctx.set("Content-Disposition", "attachment; filename="+ (+new Date()) + '.xlsx');
ctx.body = buffer;
}
module.exports = {
exportList: exportList
}
// config 格式
const config = [{
name: 'userid',
title: '用户ID',
},{
name: 'up_time',
title: '状态时间',
format: function(value) {
return value.replace(/\.\d+/, '');
}
},{
name: 'appl_status_byhand',
// name: 'appl_status_byhand_desc',
title: '人审状态',
format: function(value) {
return value && manualStatus[value] && manualStatus[value].text || '-'
}
},{
name: 'available_quota/credit_quota',
title: '剩余额度/授信额度',
format: function(value, item) {
return `¥${item.available_quota/100 || 0}/¥${item.credit_quota/100 || 0}`
}
},{
name: 'user_type_desc',
title: '名单类型',
format: function(value, item) {
if(item.user_type) {
item.user_type.forEach(user => {
userTypeText.push(user_type_define[user] || '-');
});
return userTypeText.join('|');
} else {
return '';
}
}
}]