home / skills / aidotnet / moyucode / exceljs

This skill reads, manipulates, and writes Excel files with full styling support, enabling fast data processing and report generation.

npx playbooks add skill aidotnet/moyucode --skill exceljs

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.8 KB
---
name: exceljs
description: 在Node.js中读取、操作和写入Excel电子表格(XLSX)。完全支持样式、公式、图表和大文件流式处理。
metadata:
  short-description: Excel电子表格操作
source:
  repository: https://github.com/exceljs/exceljs
  license: MIT
  stars: 14k+
---

# ExcelJS Tool

## Description
Read, manipulate, and write Excel spreadsheets with full formatting support.

## Source
- Repository: [exceljs/exceljs](https://github.com/exceljs/exceljs)
- License: MIT

## Installation

```bash
npm install exceljs
```

## Usage Examples

### Create Excel File

```typescript
import ExcelJS from 'exceljs';

async function createReport() {
  const workbook = new ExcelJS.Workbook();
  workbook.creator = 'My App';
  workbook.created = new Date();
  
  const sheet = workbook.addWorksheet('Sales Report');
  
  // Define columns
  sheet.columns = [
    { header: 'ID', key: 'id', width: 10 },
    { header: 'Product', key: 'product', width: 30 },
    { header: 'Quantity', key: 'quantity', width: 15 },
    { header: 'Price', key: 'price', width: 15 },
    { header: 'Total', key: 'total', width: 15 },
  ];
  
  // Add data
  const data = [
    { id: 1, product: 'Widget A', quantity: 100, price: 9.99 },
    { id: 2, product: 'Widget B', quantity: 50, price: 19.99 },
    { id: 3, product: 'Widget C', quantity: 75, price: 14.99 },
  ];
  
  data.forEach(item => {
    sheet.addRow({
      ...item,
      total: { formula: `C${sheet.rowCount + 1}*D${sheet.rowCount + 1}` },
    });
  });
  
  await workbook.xlsx.writeFile('report.xlsx');
}
```

### Style Cells

```typescript
async function createStyledReport() {
  const workbook = new ExcelJS.Workbook();
  const sheet = workbook.addWorksheet('Styled');
  
  // Header row with styling
  const headerRow = sheet.addRow(['Name', 'Email', 'Status']);
  headerRow.eachCell(cell => {
    cell.fill = {
      type: 'pattern',
      pattern: 'solid',
      fgColor: { argb: '4F46E5' },
    };
    cell.font = { color: { argb: 'FFFFFF' }, bold: true };
    cell.alignment = { horizontal: 'center' };
    cell.border = {
      top: { style: 'thin' },
      bottom: { style: 'thin' },
    };
  });
  
  // Data rows
  const users = [
    { name: 'John', email: '[email protected]', status: 'Active' },
    { name: 'Jane', email: '[email protected]', status: 'Inactive' },
  ];
  
  users.forEach(user => {
    const row = sheet.addRow([user.name, user.email, user.status]);
    
    // Conditional formatting
    const statusCell = row.getCell(3);
    statusCell.fill = {
      type: 'pattern',
      pattern: 'solid',
      fgColor: { argb: user.status === 'Active' ? '22C55E' : 'EF4444' },
    };
  });
  
  await workbook.xlsx.writeFile('styled-report.xlsx');
}
```

### Read Excel File

```typescript
async function readExcel(filePath: string) {
  const workbook = new ExcelJS.Workbook();
  await workbook.xlsx.readFile(filePath);
  
  const sheet = workbook.getWorksheet('Sheet1');
  const data: any[] = [];
  
  sheet?.eachRow((row, rowNumber) => {
    if (rowNumber === 1) return; // Skip header
    
    data.push({
      id: row.getCell(1).value,
      name: row.getCell(2).value,
      email: row.getCell(3).value,
    });
  });
  
  return data;
}
```

### Stream Large Files

```typescript
async function streamLargeExcel(data: any[], outputPath: string) {
  const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({
    filename: outputPath,
    useStyles: true,
  });
  
  const sheet = workbook.addWorksheet('Data');
  sheet.columns = [
    { header: 'ID', key: 'id' },
    { header: 'Value', key: 'value' },
  ];
  
  // Stream rows (memory efficient)
  for (const item of data) {
    sheet.addRow(item).commit();
  }
  
  await workbook.commit();
}
```

## Tags
`excel`, `spreadsheet`, `xlsx`, `report`, `data-export`

## Compatibility
- Codex: ✅
- Claude Code: ✅

Overview

This skill provides a TypeScript-friendly interface to read, modify, and write Excel (.xlsx) files in Node.js with full support for styles, formulas, charts, and streaming large files. It covers creating workbooks and worksheets, styling cells, adding formulas, reading rows, and memory-efficient streaming for very large exports. Use it to generate reports, transform spreadsheets, or process Excel data server-side.

How this skill works

You instantiate a Workbook, add or load worksheets, then manipulate rows, columns, cells, styles, and formulas via a clear API. For large datasets, use the streaming WorkbookWriter to write rows and commit them incrementally to avoid high memory usage. Reading is done by loading an .xlsx file into a Workbook and iterating rows; writing supports full styling and Excel features before saving to disk or stream.

When to use it

  • Generate styled reports or invoices from server-side data
  • Read and transform uploaded Excel files into JSON or database records
  • Stream-export millions of rows without exhausting memory
  • Add formulas, conditional styles, or charts to programmatically created spreadsheets
  • Automate recurring Excel file generation in backend jobs or APIs

Best practices

  • Use WorkbookWriter for large exports to keep memory usage low
  • Define sheet.columns with keys to simplify adding rows as objects
  • Apply styles and borders sparingly when exporting very large files to reduce processing time
  • Commit streamed rows immediately (row.commit()) and call workbook.commit() at the end
  • Skip the header row when reading if you need only data rows, and validate cell types before use

Example use cases

  • Server generates monthly sales reports with styled headers, totals as formulas, and saves XLSX for download
  • Import user lists from uploaded spreadsheets into a database after validating and mapping columns
  • Produce large data exports (logs, analytics) via streaming writer to avoid OOM errors
  • Create templated invoice files with company branding, number formatting, and calculated totals
  • Apply conditional fills to status columns to create visually clear dashboards for stakeholders

FAQ

Can I stream-write files without loading everything into memory?

Yes. Use ExcelJS.stream.xlsx.WorkbookWriter to write rows and commit them incrementally, then call workbook.commit() to finalize the file.

Does the library support cell styling and formulas?

Yes. You can set fonts, fills, borders, alignment, and insert formulas; these are preserved when saving the workbook.

Is this suitable for reading very large Excel files?

Reading very large files may still be memory intensive; streaming is primarily for writing large files. For large reads, consider parsing in chunks and only loading required worksheets or ranges.