$npx -y skills add sabahattink/antigravity-fullstack-hq --skill xlsx-officialGenerating Excel files with xlsx/exceljs in Node.js. Use when generating .xlsx reports, data exports, dashboards, or spreadsheets from database data.
| 1 | # Excel Generation |
| 2 | |
| 3 | ## Option A — ExcelJS (Feature-Rich) |
| 4 | |
| 5 | ```bash |
| 6 | npm install exceljs |
| 7 | npm install -D @types/node |
| 8 | ``` |
| 9 | |
| 10 | ```typescript |
| 11 | // lib/excel/report.excel.ts |
| 12 | import ExcelJS from 'exceljs' |
| 13 | |
| 14 | export async function generateExcelReport(data: ReportData): Promise<Buffer> { |
| 15 | const workbook = new ExcelJS.Workbook() |
| 16 | workbook.creator = 'Antigravity HQ' |
| 17 | workbook.lastModifiedBy = 'system' |
| 18 | workbook.created = new Date() |
| 19 | workbook.modified = new Date() |
| 20 | |
| 21 | const sheet = workbook.addWorksheet('Report', { |
| 22 | pageSetup: { |
| 23 | paperSize: 9, // A4 |
| 24 | orientation: 'landscape', |
| 25 | fitToPage: true, |
| 26 | }, |
| 27 | views: [{ state: 'frozen', xSplit: 0, ySplit: 1 }], // freeze header row |
| 28 | }) |
| 29 | |
| 30 | // ─── Column Definitions ─────────────────────────────────── |
| 31 | sheet.columns = data.columns.map(col => ({ |
| 32 | header: col.header, |
| 33 | key: col.key, |
| 34 | width: col.width ?? 20, |
| 35 | style: { |
| 36 | alignment: { horizontal: col.align ?? 'left', wrapText: false }, |
| 37 | numFmt: col.format, // e.g. '#,##0.00', 'yyyy-mm-dd' |
| 38 | }, |
| 39 | })) |
| 40 | |
| 41 | // ─── Style the Header Row ───────────────────────────────── |
| 42 | const headerRow = sheet.getRow(1) |
| 43 | headerRow.eachCell(cell => { |
| 44 | cell.fill = { |
| 45 | type: 'pattern', |
| 46 | pattern: 'solid', |
| 47 | fgColor: { argb: 'FF2563EB' }, |
| 48 | } |
| 49 | cell.font = { |
| 50 | name: 'Calibri', |
| 51 | size: 11, |
| 52 | bold: true, |
| 53 | color: { argb: 'FFFFFFFF' }, |
| 54 | } |
| 55 | cell.border = { |
| 56 | bottom: { style: 'medium', color: { argb: 'FF1D4ED8' } }, |
| 57 | } |
| 58 | }) |
| 59 | headerRow.height = 24 |
| 60 | |
| 61 | // ─── Add Data Rows ──────────────────────────────────────── |
| 62 | data.rows.forEach((row, index) => { |
| 63 | const excelRow = sheet.addRow(row) |
| 64 | excelRow.height = 18 |
| 65 | |
| 66 | // Alternating row colors |
| 67 | if (index % 2 === 0) { |
| 68 | excelRow.eachCell(cell => { |
| 69 | cell.fill = { |
| 70 | type: 'pattern', |
| 71 | pattern: 'solid', |
| 72 | fgColor: { argb: 'FFF8FAFC' }, |
| 73 | } |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | excelRow.eachCell(cell => { |
| 78 | cell.font = { name: 'Calibri', size: 10 } |
| 79 | cell.border = { |
| 80 | bottom: { style: 'thin', color: { argb: 'FFE5E7EB' } }, |
| 81 | } |
| 82 | }) |
| 83 | }) |
| 84 | |
| 85 | // ─── Summary Row ────────────────────────────────────────── |
| 86 | const summaryRow = sheet.addRow(data.summary) |
| 87 | summaryRow.eachCell(cell => { |
| 88 | cell.font = { name: 'Calibri', size: 10, bold: true } |
| 89 | cell.fill = { |
| 90 | type: 'pattern', |
| 91 | pattern: 'solid', |
| 92 | fgColor: { argb: 'FFEFF6FF' }, |
| 93 | } |
| 94 | cell.border = { |
| 95 | top: { style: 'medium', color: { argb: 'FF2563EB' } }, |
| 96 | bottom: { style: 'medium', color: { argb: 'FF2563EB' } }, |
| 97 | } |
| 98 | }) |
| 99 | |
| 100 | // ─── Auto-filter ────────────────────────────────────────── |
| 101 | sheet.autoFilter = { |
| 102 | from: { row: 1, column: 1 }, |
| 103 | to: { row: 1, column: data.columns.length }, |
| 104 | } |
| 105 | |
| 106 | // ─── Return Buffer ──────────────────────────────────────── |
| 107 | return workbook.xlsx.writeBuffer() as Promise<Buffer> |
| 108 | } |
| 109 | ``` |
| 110 | |
| 111 | ## Multiple Sheets |
| 112 | |
| 113 | ```typescript |
| 114 | async function generateMultiSheetReport(report: MultiSheetReport): Promise<Buffer> { |
| 115 | const workbook = new ExcelJS.Workbook() |
| 116 | |
| 117 | // Summary sheet |
| 118 | const summarySheet = workbook.addWorksheet('Summary') |
| 119 | addSummarySheet(summarySheet, report.summary) |
| 120 | |
| 121 | // Data sheets |
| 122 | for (const section of report.sections) { |
| 123 | const sheet = workbook.addWorksheet(section.name.substring(0, 31)) // Excel 31-char limit |
| 124 | addDataSheet(sheet, section.columns, section.rows) |
| 125 | } |
| 126 | |
| 127 | // Charts sheet (using data from other sheets) |
| 128 | const chartSheet = workbook.addWorksheet('Charts') |
| 129 | addChart(workbook, chartSheet, report) |
| 130 | |
| 131 | return workbook.xlsx.writeBuffer() as Promise<Buffer> |
| 132 | } |
| 133 | ``` |
| 134 | |
| 135 | ## Charts in ExcelJS |
| 136 | |
| 137 | ```typescript |
| 138 | function addChart(workbook: ExcelJS.Workbook, sheet: ExcelJS.Worksheet, report: Report) { |
| 139 | // ExcelJS doesn't support charts directly — use chartjs-to-image or |
| 140 | // embed a pre-generated chart image |
| 141 | |
| 142 | // Generate chart as PNG buffer (using chartjs-node-canvas) |
| 143 | const chartImage = generateChartPng(report.chartData) |
| 144 | |
| 145 | const imageId = workbook.addImage({ |
| 146 | buffer: chartImage, |
| 147 | extension: 'png', |
| 148 | }) |
| 149 | |
| 150 | sheet.addImage(imageId, { |
| 151 | tl: { col: 0, row: 0 }, |
| 152 | ext: { width: 800, height: 400 }, |
| 153 | editAs: 'oneCell', |
| 154 | }) |
| 155 | } |
| 156 | ``` |
| 157 | |
| 158 | ## Option B — SheetJS (xlsx) — Simpler |
| 159 | |
| 160 | ```bash |
| 161 | npm install xlsx |
| 162 | npm install -D @types/xlsx |
| 163 | ``` |
| 164 | |
| 165 | ```typescript |
| 166 | import * as XLSX from 'xlsx' |
| 167 | |
| 168 | export function generateSimpleXlsx(data: { |
| 169 | headers: string[] |
| 170 | rows: (string | number | Date)[][] |
| 171 | sheetName?: string |
| 172 | }): Buffer { |
| 173 | const wb = XLSX.utils.book_new() |
| 174 | |
| 175 | // Convert to worksheet |
| 176 | const wsData = [data.headers, ...data.rows] |
| 177 | const ws = XLSX.utils.aoa_to_sheet(wsData) |
| 178 | |
| 179 | // Set column widths |
| 180 | ws['!cols'] = data.headers.map(() => ({ wch: 20 })) |
| 181 | |
| 182 | // Freeze top row |
| 183 | ws['!freeze'] = { xSplit: 0, ySplit: 1 } |
| 184 | |
| 185 | XLSX.utils.book_appen |