个人中心页面
This commit is contained in:
@@ -130,3 +130,40 @@ export function getWeatherType(weatherText = "") {
|
||||
// 兜底
|
||||
return "cloudy";
|
||||
}
|
||||
|
||||
// ==============================================
|
||||
// 通用导出表格为 CSV / Excel(通用版,不写死任何字段)
|
||||
// ==============================================
|
||||
export const exportTable = (columns, dataSource, fileName = "导出数据") => {
|
||||
// 1. 从 columns 自动提取表头(过滤掉不显示的列)
|
||||
const headers = columns
|
||||
.filter(col => col.title && col.dataIndex) // 只取有标题+有字段的列
|
||||
.map(col => col.title);
|
||||
|
||||
// 2. 自动提取数据(根据 dataIndex 取值)
|
||||
const data = dataSource.map(item => {
|
||||
return columns
|
||||
.filter(col => col.title && col.dataIndex)
|
||||
.map(col => {
|
||||
let val = item[col.dataIndex];
|
||||
// 处理 undefined / null
|
||||
if (val === undefined || val === null) return "";
|
||||
return String(val).replace(/"/g, '""'); // 转义CSV引号
|
||||
});
|
||||
});
|
||||
|
||||
// 3. 拼接CSV内容
|
||||
const csvContent = [
|
||||
headers.join(','),
|
||||
...data.map(row => `"${row.join('","')}"`)
|
||||
].join('\n');
|
||||
|
||||
// 4. 下载文件(解决中文乱码)
|
||||
const blob = new Blob(["\ufeff" + csvContent], { type: "text/csv;charset=utf-8;" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${fileName}_${dayjs().format("YYYY-MM-DD")}.csv`;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user