A React component that allows you to define Excel sheets using JSX and export them using SheetJS.
- Define spreadsheets using familiar JSX syntax
- Support for different cell types (text, number, boolean, date, formula)
- Formula support with relative cell references using the rc() function
- Custom formatting and column widths -- no more truncated columns or numbers interpreted as dates!
- Convert to SheetJS format for export into any format supported by SheetJS (including Excel)
Check out the live demo at: https://xkjyeah.github.io/react-excel-export/
The demo showcases both HTML table and Excel export functionality with syntax-highlighted source code.
This repository includes a complete Next.js example application in the examples/ directory that demonstrates:
- Basic Excel export functionality
- HTML table display with export capability
- Syntax-highlighted source code display
- Ready-to-deploy GitHub Pages configuration
Visit http://localhost:3000 to see the demo in action.
The component supports the following cell types:
- <text> - Text values
- <number> - Numeric values
- <boolean> - Boolean values
- <date> - Date values
- <formula> - Excel formulas
Each cell type supports the following properties:
- width?: number - Column width
- z?: string - Excel format string
- children - The cell value
Rows support the following properties:
- widthSetting?: boolean - Whether this row defines column widths
The SheetJsOutput component exposes a ref with a getExcelSheet() function that returns the current Excel sheet data:
Important: The Excel sheet is only generated when this function is called. Before calling it, the function will return null.
The returned object is a SheetJS Sheet Object.
Make sure you have the required dependencies:
The component includes full TypeScript support with proper type definitions for all JSX elements and the ref interface.
The main component that renders Excel data.
Props:
- render?: (worksheet: WorkSheet) => React.ReactNode - Function to render UI with the generated worksheet
- children - Excel structure using the cell components
Represents an Excel row.
Props:
- widthSetting?: boolean - If true, this row's cell widths will be used to set column widths
- children - Cell components
Renders a text cell.
Props:
- width?: number - Column width
- z?: string - Format string
- children - Cell content
Renders a numeric cell.
Props:
- width?: number - Column width
- z?: string - Format string (e.g., "#,##0.00")
- children - Cell content
Renders a date cell.
Props:
- width?: number - Column width
- z?: string - Date format (e.g., "MMM DD", "YYYY-MM-DD")
- children - Date value
Renders a boolean cell.
Props:
- width?: number - Column width
- z?: string - Format string
- children - Boolean value
Renders a formula cell.
Props:
- width?: number - Column width
- z?: string - Format string
- children - Excel formula
The library supports Excel formulas with relative cell references using the rc() function:
Creates a relative cell reference for use in formulas.
Parameters:
- dr: number - Delta row (row offset relative to current cell)
- dc: number - Delta column (column offset relative to current cell)
Examples:
- rc(0, -1) - References the cell to the left
- rc(-1, 0) - References the cell above
- rc(0, -2) - References the cell two columns to the left
- rc(1, 1) - References the cell one row down and one column right
The rc() function generates the appropriate Excel cell reference (e.g., A1, B2) based on the current cell position during rendering.
Downloads the generated worksheet as an Excel file.
Parameters:
- worksheet - The worksheet object from SheetJsOutput
- filename - Optional filename (default: 'export.xlsx')
This library uses React's custom renderer (react-reconciler) to create a virtual representation of Excel data. Instead of rendering to the DOM, it renders to a custom data structure that can be converted to SheetJS format.
The key components (row, text, number, etc.) are not actual DOM elements but custom elements that the renderer understands and processes to build the Excel structure.
MIT