|
|
@@ -220,41 +220,40 @@ const exportToExcel = () => {
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- // 2. 从tableConfig中提取【导出字段】和【中文表头】(核心复用逻辑)
|
|
|
+ // 2. 从tableConfig中提取【导出字段(英文key)】和【中文表头】
|
|
|
const exportFields = tableConfig.map(item => item.prop); // 提取字段名:['var_name', 'val', 'ts']
|
|
|
- const chineseHeaders = tableConfig.map(item => item.label); // 提取中文表头:['点位名称', '点位状态', '时间']
|
|
|
+ const chineseHeaders = tableConfig.map(item => item.label); // 提取中文表头:
|
|
|
|
|
|
// 3. 处理原始数据:只保留tableConfig中配置的字段,空值兜底
|
|
|
const processedData = allData.value.map(item => {
|
|
|
- const row : Record<string, any> = {};
|
|
|
+ const row: Record<string, any> = {};
|
|
|
exportFields.forEach(field => {
|
|
|
const typedItem = item as Record<string, any>;
|
|
|
- row[field] = typedItem[field] ?? ''; // 空值替换为'',避免undefined
|
|
|
+ row[field] = typedItem[field] ?? '';
|
|
|
});
|
|
|
return row;
|
|
|
});
|
|
|
|
|
|
- // 4. 生成Excel工作表(复用tableConfig的表头)
|
|
|
+ // 关键修复:header用英文key(保证数据列匹配),不跳过默认表头(表头行是英文key)
|
|
|
const worksheet = XLSX.utils.json_to_sheet(processedData, {
|
|
|
- header: exportFields, // 匹配tableConfig的字段名
|
|
|
- skipHeader: true, // 跳过默认的英文字段表头
|
|
|
+ header: exportFields, // 按exportFields顺序生成列,匹配processedData的key
|
|
|
+ skipHeader: false, // 保留默认表头行(此时表头是英文key,后续替换为中文)
|
|
|
});
|
|
|
|
|
|
- // 5. 手动设置中文表头(从tableConfig取label)
|
|
|
+ // 替换表头行(第0行)的英文key为中文
|
|
|
chineseHeaders.forEach((header, index) => {
|
|
|
const cellRef = XLSX.utils.encode_cell({ r: 0, c: index }); // 0行=表头行,index=列索引
|
|
|
- worksheet[cellRef] = { v: header, t: 's' }; // 设置表头内容为tableConfig的label
|
|
|
+ worksheet[cellRef] = { v: header, t: 's' }; // 覆盖为中文表头
|
|
|
});
|
|
|
|
|
|
// 6. 生成工作簿并导出
|
|
|
const workbook = XLSX.utils.book_new();
|
|
|
- XLSX.utils.book_append_sheet(workbook, worksheet, '历史数据'); // 自定义sheet名
|
|
|
- // 文件名加时间戳,避免覆盖
|
|
|
- const fileName = craneInfo.crane_name+`历史数据.xlsx`;
|
|
|
+ XLSX.utils.book_append_sheet(workbook, worksheet, '历史数据');
|
|
|
+ const fileName = craneInfo.crane_name + `历史数据.xlsx`;
|
|
|
XLSX.writeFile(workbook, fileName);
|
|
|
|
|
|
ElMessage.success('Excel导出成功!');
|
|
|
- } catch (error:any) {
|
|
|
+ } catch (error: any) {
|
|
|
ElMessage.error(`导出失败:${error.message}`);
|
|
|
}
|
|
|
};
|