软件列表导出的excel文件打开没有数据

This commit is contained in:
付民康
2025-03-26 17:12:43 +08:00
parent c588545227
commit a78585ff1b

View File

@@ -503,56 +503,56 @@ onMounted(async () => {
// getReleaseList();
});
const exportResult = async () => {
const { data, error } = await getReleaseExport({
softwareName: newFormData.value.softwareName || '',
searchType: newFormData.value.searchType || '',
softwareVersion: newFormData.value.softwareVersion || '',
programmingLanguage: newFormData.value.programmingLanguage || '',
interestTag: newFormData.value.interestTag || '',
industry: newFormData.value.industry || '',
licenseName: newFormData.value.licenseName || '',
dependentSoftware: newFormData.value.dependentSoftware || '',
releaseDateStart:
newFormData.value.dateRange && newFormData.value.dateRange.length > 0 ? newFormData.value.dateRange[0] : '',
releaseDateEnd:
newFormData.value.dateRange && newFormData.value.dateRange.length > 0 ? newFormData.value.dateRange[1] : '',
current: pager.value.pageIndex,
size: pager.value.pageSize
});
if (data && !error) {
debugger
// 方法1如果后端返回的是文件流Blob
const blob = new Blob([data.data]);
const downloadUrl = window.URL.createObjectURL(blob);
// 创建一个临时的a标签触发下载
const link = document.createElement('a');
link.href = downloadUrl;
// 从Content-Disposition头中获取文件名或者使用默认文件名
const contentDisposition = data.headers['content-disposition'];
let fileName = 'export.xlsx'; // 默认文件名
if (contentDisposition) {
const fileNameMatch = contentDisposition.match(/filename="?(.+)"?/);
if (fileNameMatch && fileNameMatch[1]) {
fileName = fileNameMatch[1].replace(/^[^0-9]*|[^0-9.xlsx]*$/g, "");
}
}
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
// 清理
document.body.removeChild(link);
window.URL.revokeObjectURL(downloadUrl);
} else {
console.error('导出失败:', error);
// 这里可以添加错误提示比如使用Element Plus的ElMessage
ElMessage.error('文件导出失败');
}
};
// const exportResult = async () => {
// const { data, error } = await getReleaseExport({
// softwareName: newFormData.value.softwareName || '',
// searchType: newFormData.value.searchType || '',
// softwareVersion: newFormData.value.softwareVersion || '',
// programmingLanguage: newFormData.value.programmingLanguage || '',
// interestTag: newFormData.value.interestTag || '',
// industry: newFormData.value.industry || '',
// licenseName: newFormData.value.licenseName || '',
// dependentSoftware: newFormData.value.dependentSoftware || '',
// releaseDateStart:
// newFormData.value.dateRange && newFormData.value.dateRange.length > 0 ? newFormData.value.dateRange[0] : '',
// releaseDateEnd:
// newFormData.value.dateRange && newFormData.value.dateRange.length > 0 ? newFormData.value.dateRange[1] : '',
// current: pager.value.pageIndex,
// size: pager.value.pageSize
// });
// if (data && !error) {
// debugger
// // 方法1如果后端返回的是文件流Blob
// const blob = new Blob([data.data]);
// const downloadUrl = window.URL.createObjectURL(blob);
//
// // 创建一个临时的a标签触发下载
// const link = document.createElement('a');
// link.href = downloadUrl;
//
// // 从Content-Disposition头中获取文件名或者使用默认文件名
// const contentDisposition = data.headers['content-disposition'];
// let fileName = 'export.xlsx'; // 默认文件名
// if (contentDisposition) {
// const fileNameMatch = contentDisposition.match(/filename="?(.+)"?/);
// if (fileNameMatch && fileNameMatch[1]) {
// fileName = fileNameMatch[1].replace(/^[^0-9]*|[^0-9.xlsx]*$/g, "");
// }
// }
//
// link.setAttribute('download', fileName);
// document.body.appendChild(link);
// link.click();
//
// // 清理
// document.body.removeChild(link);
// window.URL.revokeObjectURL(downloadUrl);
// } else {
// console.error('导出失败:', error);
// // 这里可以添加错误提示比如使用Element Plus的ElMessage
// ElMessage.error('文件导出失败');
// }
// };
// onMounted(() => {
// const query: any = route.query;
@@ -561,47 +561,47 @@ const exportResult = async () => {
// };
// });
// const exportResult = () => {
// const selectDatas = filterTableRef.value.store.getCheckedRows().map((d) => d.id);
// if (!allTableData.value.length || selectDatas.length <= 0) {
// Message.info('未选择任何数据');
// return;
// }
//
// const exportData = allTableData.value
// .filter((d) => {
// return selectDatas.includes(d.id);
// })
// .map((item) => {
// let res = {};
// columns.value.forEach((col) => {
// if (col.key === 'validStatus') {
// res[col.label] = validStatusMap[item[col.key]] ? validStatusMap[item[col.key]] : '';
// } else {
// res[col.label] = item[col.key] ? item[col.key] : '';
// }
// });
// return res;
// });
//
// try {
// const workSheet = XLSX.utils.json_to_sheet(exportData);
// const workBook: any = { Sheets: { 校验结果: workSheet }, SheetNames: ['校验结果'] };
// const excelBuffer = XLSX.write(workBook, { bookType: 'xlsx', type: 'array' });
//
// const link = document.createElement('a');
// const blob = new Blob([excelBuffer]);
// link.setAttribute('href', window.URL.createObjectURL(blob));
// link.setAttribute('download', '软件存在及可信校验结果.xlsx');
// link.style.visibility = 'hidden';
// document.body.appendChild(link);
// link.click();
// document.body.removeChild(link);
// Message.success('导出成功');
// } catch {
// Message.error('导出失败');
// }
// };
const exportResult = () => {
const selectDatas = filterTableRef.value.store.getCheckedRows().map((d) => d.id);
if (!allTableData.value.length || selectDatas.length <= 0) {
Message.info('未选择任何数据');
return;
}
const exportData = allTableData.value
.filter((d) => {
return selectDatas.includes(d.id);
})
.map((item) => {
let res = {};
columns.value.forEach((col) => {
if (col.key === 'validStatus') {
res[col.label] = validStatusMap[item[col.key]] ? validStatusMap[item[col.key]] : '';
} else {
res[col.label] = item[col.key] ? item[col.key] : '';
}
});
return res;
});
try {
const workSheet = XLSX.utils.json_to_sheet(exportData);
const workBook: any = { Sheets: { 校验结果: workSheet }, SheetNames: ['校验结果'] };
const excelBuffer = XLSX.write(workBook, { bookType: 'xlsx', type: 'array' });
const link = document.createElement('a');
const blob = new Blob([excelBuffer]);
link.setAttribute('href', window.URL.createObjectURL(blob));
link.setAttribute('download', '软件存在及可信校验结果.xlsx');
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
Message.success('导出成功');
} catch {
Message.error('导出失败');
}
};
</script>
<style scoped lang="scss">