WXL
2026-05-17 b22b937bf902dcfbbf6d2cc6dc95ca47d160e199
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// src/utils/excel.js
import * as XLSX from 'xlsx';
import FileSaver from 'file-saver';
 
export function exportExcel(data, fileName = '导出数据') {
  // 创建工作簿
  const wb = XLSX.utils.book_new();
 
  // 创建工作表
  const ws = XLSX.utils.aoa_to_sheet(data);
 
  // 添加到工作簿
  XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
 
  // 生成Excel文件
  const wbout = XLSX.write(wb, {
    bookType: 'xlsx',
    type: 'array'
  });
 
  // 创建Blob对象
  const blob = new Blob([wbout], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  });
 
  // 保存文件
  FileSaver.saveAs(blob, `${fileName}_${new Date().getTime()}.xlsx`);
}
 
export function exportJsonToExcel(jsonData, headers, fileName = '导出数据') {
  const data = [headers];
 
  jsonData.forEach(item => {
    const row = headers.map(header => {
      if (typeof header === 'object') {
        return item[header.key] || '';
      }
      return item[header] || '';
    });
    data.push(row);
  });
 
  exportExcel(data, fileName);
}