exceljs

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

ExcelJS Tool

ExcelJS 工具

Description

说明

Read, manipulate, and write Excel spreadsheets with full formatting support.
读取、操作和写入Excel电子表格,完全支持格式设置。

Source

来源

Installation

安装

bash
npm install exceljs
bash
npm install exceljs

Usage Examples

使用示例

Create Excel File

创建Excel文件

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');
}
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');
  
  // 定义列
  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 },
  ];
  
  // 添加数据
  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: 'john@example.com', status: 'Active' },
    { name: 'Jane', email: 'jane@example.com', 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');
}
typescript
async function createStyledReport() {
  const workbook = new ExcelJS.Workbook();
  const sheet = workbook.addWorksheet('Styled');
  
  // 带样式的表头行
  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' },
    };
  });
  
  // 数据行
  const users = [
    { name: 'John', email: 'john@example.com', status: 'Active' },
    { name: 'Jane', email: 'jane@example.com', status: 'Inactive' },
  ];
  
  users.forEach(user => {
    const row = sheet.addRow([user.name, user.email, user.status]);
    
    // 条件格式设置
    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

读取Excel文件

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;
}
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; // 跳过表头
    
    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();
}
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' },
  ];
  
  // 流式写入行(内存高效)
  for (const item of data) {
    sheet.addRow(item).commit();
  }
  
  await workbook.commit();
}

Tags

标签

excel
,
spreadsheet
,
xlsx
,
report
,
data-export
excel
,
spreadsheet
,
xlsx
,
report
,
data-export

Compatibility

兼容性

  • Codex: ✅
  • Claude Code: ✅
  • Codex: ✅
  • Claude Code: ✅