write_excel
Export CSV or JSON data to Excel files with customizable sheet names for structured data storage and analysis.
Instructions
Write data to an Excel file.
Args:
file_path: Path to save the Excel file
data: Data in CSV or JSON format
sheet_name: Name of the sheet (for Excel files)
format: Format of the input data ('csv' or 'json')
Returns:
Confirmation message
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| data | Yes | ||
| sheet_name | No | Sheet1 | |
| format | No | csv |
Implementation Reference
- mcp_excel_server/server.py:212-252 (handler)The main handler function for the 'write_excel' tool. Decorated with @mcp.tool(), it reads input data as CSV or JSON into a pandas DataFrame and writes it to the specified file_path in Excel, CSV, TSV, or JSON format based on the extension. Returns success or error message.@mcp.tool() def write_excel(file_path: str, data: str, sheet_name: Optional[str] = "Sheet1", format: Optional[str] = "csv") -> str: """ Write data to an Excel file. Args: file_path: Path to save the Excel file data: Data in CSV or JSON format sheet_name: Name of the sheet (for Excel files) format: Format of the input data ('csv' or 'json') Returns: Confirmation message """ try: if format.lower() == 'csv': df = pd.read_csv(io.StringIO(data)) elif format.lower() == 'json': df = pd.read_json(io.StringIO(data)) else: return f"Unsupported data format: {format}" _, ext = os.path.splitext(file_path) ext = ext.lower() if ext in ['.xlsx', '.xls', '.xlsm']: df.to_excel(file_path, sheet_name=sheet_name, index=False) elif ext == '.csv': df.to_csv(file_path, index=False) elif ext == '.tsv': df.to_csv(file_path, sep='\t', index=False) elif ext == '.json': df.to_json(file_path, orient='records') else: return f"Unsupported output file extension: {ext}" return f"Successfully wrote data to {file_path}" except Exception as e: return f"Error writing data: {str(e)}"
- mcp_excel_server/server.py:212-212 (registration)The @mcp.tool() decorator registers the write_excel function as an MCP tool.@mcp.tool()