Skip to main content
Glama
zhiwei5576

Excel MCP Server

by zhiwei5576

writeSheetData

Create Excel files by writing structured data to worksheets, generating spreadsheets from JSON data with custom sheet and column names.

Instructions

Create a new Excel file with provided data

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fileAbsolutePathYesThe absolute path for the new Excel file
dataYesData object with dynamic sheet names and column names

Implementation Reference

  • The core handler function that creates a new Excel workbook, iterates over the data object to create worksheets for each sheet, and writes the file using XLSX library.
    export async function writeSheetData( filePathWithName: string, data: SheetData ): Promise<boolean> { try { // 创建新的工作簿 const workbook = XLSX.utils.book_new(); // 遍历数据对象的每个工作表 for (const [sheetName, sheetData] of Object.entries(data)) { // 将数据转换为工作表 const worksheet = XLSX.utils.json_to_sheet(sheetData); // 将工作表添加到工作簿 XLSX.utils.book_append_sheet(workbook, worksheet, sheetName); } // 写入文件 XLSX.writeFile(workbook, filePathWithName); return true; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); throw new Error(`Failed to write Excel data: ${errorMessage}`); } }
  • Registers the writeSheetData tool with Zod input schema, input validation, path normalization, file existence check, and calls the core writeSheetData handler.
    server.tool("writeSheetData", 'Create a new Excel file with provided data', { fileAbsolutePath: z.string().describe("The absolute path for the new Excel file"), data: z.record( z.string(), // 表名(动态) z.array( // 表数据数组 z.record( // 每行数据对象 z.string(), // 字段名(动态) z.any() // 字段值(任意类型) ) ) ).describe("Data object with dynamic sheet names and column names") }, async (params: { fileAbsolutePath: string; data: Record<string, Record<string, any>[]>; }) => { try { const normalizedPath = await normalizePath(params.fileAbsolutePath); if (normalizedPath === 'error') { return { content: [{ type: "text", text: JSON.stringify({ error: `Invalid file path: ${params.fileAbsolutePath}`, suggestion: "Please verify the file path and name" }) }] }; } // 校验数据结构 if (Object.keys(params.data).length === 0) { return { content: [{ type: "text", text: JSON.stringify({ error: "Empty data object provided", suggestion: "Please provide at least one sheet with data" }) }] }; } // 校验每个表的数据 for (const [sheetName, sheetData] of Object.entries(params.data)) { if (!Array.isArray(sheetData) || sheetData.length === 0) { return { content: [{ type: "text", text: JSON.stringify({ error: `Invalid data format in sheet "${sheetName}": Data must be a non-empty array`, suggestion: "Please check the data format of each sheet" }) }] }; } } if (await fileExists(normalizedPath)) { return { content: [{ type: "text", text: JSON.stringify({ error: `File already exists: ${params.fileAbsolutePath}`, suggestion: "Please specify a different file path" }) }] }; } await writeSheetData(normalizedPath, params.data); return { content: [{ type: "text", text: JSON.stringify({ success: true, message: `Excel file created successfully: ${normalizedPath}` }) }] }; } catch (error) { return { content: [{ type: "text", text: JSON.stringify({ error: `Failed to write Excel data: ${error}`, suggestion: "Please verify the data format and file path" }) }] }; } } ); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/zhiwei5576/excel-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server