mcpField
E
exceljs

exceljs

github.com/aidotnet文档处理85186 次安装39 周前更新
代码仓库

关于 exceljs

ExcelJS 是用于读取、编辑和写入 Excel 电子表格的 JavaScript 库,支持单元格格式设置。它面向构建电子表格处理流程的开发者,可用于生成报表和输出大型文件。

主要功能

  1. 01读取 Excel 文件
  2. 02创建和编辑工作簿
  3. 03设置单元格格式
  4. 04流式处理大型文件

适用场景

  1. 01读取电子表格数据
  2. 02生成格式化报表
  3. 03写入大型 Excel 文件

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/aidotnet/moyucode --skill exceljs

安装后在 Claude Code 中输入/exceljs确认已加载。

详细介绍

ExcelJS 工具

描述

读取、操作和写入 Excel 电子表格,并全面支持格式设置。

来源

安装

npm install exceljs

使用示例

创建 Excel 文件
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');
}
设置单元格样式
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');
}
读取 Excel 文件
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;
}
流式处理大型文件
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();
}

标签

excel、spreadsheet、xlsx、report、data-export

兼容性

  • Codex:✅
  • Claude Code:✅

评论

支持 Markdown