export.ts
1 import dayjs from 'dayjs'; 2 3 /** 4 * Generic CSV export utility 5 * @param headers - Column header labels 6 * @param rows - 2D array of cell values 7 * @param filename - Output filename (without extension and date suffix) 8 */ 9 export function exportCSV( 10 headers: string[], 11 rows: (string | number | null | undefined)[][], 12 filename: string, 13 ): void { 14 const BOM = '\uFEFF'; 15 const csv = 16 BOM + 17 [headers, ...rows] 18 .map((r) => 19 r.map((c) => `"${String(c ?? '').replace(/"/g, '""')}"`).join(','), 20 ) 21 .join('\n'); 22 23 const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' }); 24 const url = URL.createObjectURL(blob); 25 const a = document.createElement('a'); 26 a.href = url; 27 a.download = `${filename}-${dayjs().format('YYYYMMDD-HHmmss')}.csv`; 28 a.click(); 29 URL.revokeObjectURL(url); 30 }