将数据以XLSX的形式导出需要安装XLSX.js文件
npm install xlsx --save
exportData(){
let table='考勤查看';
let page=1;
let rows=this.totals;
let exportTableCellColumn={
xuhao:'序号',
title:'签到活动标题',
userName:'签到用户名',
uStartTime:'用户签到时间',
uEntTime:'用户签退时间',
lateReason:'迟到原因',
retreatReason:'早退原因',
};
let userId=JSON.parse(localStorage.getItem('userInfo')).id;
let params={
page,
rows,
userId:this.$store.state.user.userInfo.id,
createBy:userId
};
this.$Request.get('api/attendance/list',params).then(res => {
if (res && res.code === 0) {
let exportTableData=[];
for(let j=0;j<res.data.list.length;j++){
let aa={
xuhao:j+1,
title:res.data.list[j].title,
userName:res.data.list[j].userName,
uStartTime:moment(res.data.list[j].uStartTime).format('YYYY-MM-DD'),
uEntTime:moment(res.data.list[j].uEntTime).format('YYYY-MM-DD'),
lateReason:res.data.list[j].lateReason,
retreatReason:res.data.list[j].retreatReason,
};
exportTableData.push(aa);
};
exportData(exportTableData,exportTableCellColumn,table);
}
});
},
data.js
//
import XLSX from 'xlsx';
//data是数据
//cloum是表头
//table是表名
export const exportData = (data,cloum,table) => {
let s2ab = function (s) {
if (typeof ArrayBuffer !== 'undefined') {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
} else {
var buf = new Array(s.length);
for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
};
const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };//这里的数据是用来定义导出的格式类
let saveas = function (obj, fileName) {//当然可以自定义简单的下载文件实现方式
var tmpa = document.createElement("a");
tmpa.download = fileName || "下载";
tmpa.href = URL.createObjectURL(obj); //绑定a标签
tmpa.click(); //模拟点击实现下载
setTimeout(function () { //延时释放
URL.revokeObjectURL(obj); //用URL.revokeObjectURL()来释放这个object URL
}, 100);
};
const wb = { SheetNames: ['Sheet1'], Sheets: {}, Props: {} };
console.log('cloum-----------------------------------------',cloum);
//, { skipHeader: true } ['申请事项', '申请人', "申请金额", "实际金额", "申请时间", "状态"]
// { header: ['申请事项', '申请人', "申请金额", "实际金额", "申请时间", "状态"] }
data.unshift(cloum);
wb.Sheets['Sheet1'] = XLSX.utils.json_to_sheet(data, { skipHeader: true } );//通过json_to_sheet转成单页(Sheet)数据
saveas(new Blob([s2ab(XLSX.write(wb, wopts))], { type: "application/octet-stream" }), table+".xlsx");
}