作者 xudada

统计费用列转数值

... ... @@ -149,11 +149,30 @@
const header = this.columns.map(col => col.label);
// 将 tableData 转换成二维数组,并添加到 worksheet 中
const data = this.tableData.map(row => this.columns.map(col => row[col.prop]));
const data = this.tableData.map(row =>
this.columns.map(col => {
// 如果是 costs 列,则转换为数值
if (col.prop === 'costs') {
return parseFloat(row[col.prop]) || 0; // 确保无效值转换为0
}
return row[col.prop];
})
);
// 使用 XLSX.utils.aoa_to_sheet 创建工作表,指定头部
const ws = XLSX.utils.aoa_to_sheet([header, ...data]);
// 指定每一列的数据类型
const columnTypes = this.columns.map(col => ({
t: col.prop === 'costs' ? 'n' : 's' // 'n' for number, 's' for string
}));
// 更新工作表中的单元格类型
for (let i = 1; i <= data.length; i++) { // 从1开始,因为第0行是标题
const cellRef = XLSX.utils.encode_cell({ c: this.columns.findIndex(col => col.prop === 'costs'), r: i });
ws[cellRef].t = 'n'; // 设置单元格类型为数值
}
// 添加 worksheet 到 workbook
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
... ...