async exportExcel() {
try {
const list: any = [];
this.multipleSelection.map((v: any) => {
list.push(v.id);
});
list.sort((a: any, b: any) => a - b);
let allApi: any = null;
if (this.showTabs == 'one') allApi = hisTruck;
if (this.showTabs == 'two') allApi = internalExport;
if (this.showTabs == 'three') allApi = procurementExport;
if (this.showTabs == 'four') allApi = downExport;
let startTime = '';
let endTime = '';
if (this.showTabs == 'one') {
if (this.infoForm.tabPosition == 'day') {
startTime = this.infoForm.day[0];
endTime = this.infoForm.day[1];
} else if (this.infoForm.tabPosition == 'week') {
startTime = this.getMonDayAndSunDay(1, this.infoForm.week) + ' 00:00:00';
endTime = this.getMonDayAndSunDay(7, this.infoForm.week) + ' 23:59:59';
} else if (this.infoForm.tabPosition == 'month') {
const date = new Date(this.infoForm.month);
startTime = yearMonth(this.infoForm.month) + ' 00:00:00';
endTime =
dayStampToTime(new Date(date.getFullYear(), date.getMonth() + 1, 0)) + ' 23:59:59'; //本月最后一天;
}
} else {
startTime = this.infoForm.time[0];
endTime = this.infoForm.time[1];
}
const res = await allApi({
startTime: startTime,
endTime: endTime,
idList: list,
materialName: this.infoForm.materialName,
statusType: this.infoForm.tabPosition,
uniqueKey: this.infoForm.only,
}).catch((error: any) => {
this.$message.error(error);
});
const fileName = decodeURIComponent(res.headers['content-disposition'].split('=')[1]).split(
"utf-8''"
)[1];
const filenameExtension: any = 'xlsx';
//封装读取文件并下载
this.readFileDownload(res, fileName, filenameExtension);
} catch (error) {
this.$message.error('导出失败');
}
}
/** 导出模板 */
readFileDownload(data: any, fileName: any, filenameExtension: any) {
const res = data.data;
//res.type 后端返回的类型
let blob: any = '';
//根据excel类型解析对应文件
if (filenameExtension === 'xlsx') {
blob = new Blob([res], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8',
});
} else if (filenameExtension === 'xls') {
blob = new Blob([res], {
type: 'application/vnd.ms-excel',
});
}
if (window.navigator && (window.navigator as any).msSaveOrOpenBlob) {
(window.navigator as any).msSaveOrOpenBlob(blob, fileName);
} else {
const elink = document.createElement('a'); // 通过elink得到一个<a>标签(有点类似于ref)
elink.download = fileName; // 规定被下载的超链接目标
elink.style.display = 'none'; // 设置样式(照抄就行了)
elink.href = URL.createObjectURL(blob); // 为<a>标签绑定href属性
document.body.appendChild(elink); // 创建一个<a>标签
elink.click();
URL.revokeObjectURL(elink.href); // 释放URL对象
document.body.removeChild(elink);
}
this.$message.success('导出成功');
// if (
// res.type === 'application/msexcel' ||
// res.type === 'application/x-download' ||
// res.type === 'text/xml' ||
// res.type === 'application/vnd.ms-excel'
// ) {
// } else {
// this.$message.warning('导出失败!');
// }
}
vue导出接口
最新推荐文章于 2024-06-06 10:02:08 发布