正在显示
1 个修改的文件
包含
20 行增加
和
1 行删除
| @@ -149,11 +149,30 @@ | @@ -149,11 +149,30 @@ | ||
| 149 | const header = this.columns.map(col => col.label); | 149 | const header = this.columns.map(col => col.label); |
| 150 | 150 | ||
| 151 | // 将 tableData 转换成二维数组,并添加到 worksheet 中 | 151 | // 将 tableData 转换成二维数组,并添加到 worksheet 中 |
| 152 | - const data = this.tableData.map(row => this.columns.map(col => row[col.prop])); | 152 | + const data = this.tableData.map(row => |
| 153 | + this.columns.map(col => { | ||
| 154 | + // 如果是 costs 列,则转换为数值 | ||
| 155 | + if (col.prop === 'costs') { | ||
| 156 | + return parseFloat(row[col.prop]) || 0; // 确保无效值转换为0 | ||
| 157 | + } | ||
| 158 | + return row[col.prop]; | ||
| 159 | + }) | ||
| 160 | + ); | ||
| 153 | 161 | ||
| 154 | // 使用 XLSX.utils.aoa_to_sheet 创建工作表,指定头部 | 162 | // 使用 XLSX.utils.aoa_to_sheet 创建工作表,指定头部 |
| 155 | const ws = XLSX.utils.aoa_to_sheet([header, ...data]); | 163 | const ws = XLSX.utils.aoa_to_sheet([header, ...data]); |
| 156 | 164 | ||
| 165 | + // 指定每一列的数据类型 | ||
| 166 | + const columnTypes = this.columns.map(col => ({ | ||
| 167 | + t: col.prop === 'costs' ? 'n' : 's' // 'n' for number, 's' for string | ||
| 168 | + })); | ||
| 169 | + | ||
| 170 | + // 更新工作表中的单元格类型 | ||
| 171 | + for (let i = 1; i <= data.length; i++) { // 从1开始,因为第0行是标题 | ||
| 172 | + const cellRef = XLSX.utils.encode_cell({ c: this.columns.findIndex(col => col.prop === 'costs'), r: i }); | ||
| 173 | + ws[cellRef].t = 'n'; // 设置单元格类型为数值 | ||
| 174 | + } | ||
| 175 | + | ||
| 157 | // 添加 worksheet 到 workbook | 176 | // 添加 worksheet 到 workbook |
| 158 | XLSX.utils.book_append_sheet(wb, ws, 'Sheet1'); | 177 | XLSX.utils.book_append_sheet(wb, ws, 'Sheet1'); |
| 159 | 178 |
-
请 注册 或 登录 后发表评论