Below we illustrate how you might import an Excel spreadsheet into AG Grid using a third-party library - in this example we're using xlsx.
Auto-Generate Columns is used so no column definitions need to be provided upfront — columns are created from the imported data. The processFileInput callback parses dropped Excel files using the same library.
Click Load Sample Excel to fetch a sample spreadsheet, or drag your own .xlsx file onto the grid. Click Upload File to re-show the file input overlay. The spreadsheet can also be downloaded here.
Example Import Copy Link
"use client";
import React, {
useCallback,
useMemo,
useRef,
useState,
StrictMode,
} from "react";
import { createRoot } from "react-dom/client";
import { AgGridReact, AgGridProvider } from "ag-grid-react";
import "./styles.css";
import {
AutoGenerateColumnDefsOptions,
AutoGenerateColumnsModule,
ClientSideRowModelModule,
ColDef,
GridApi,
GridOptions,
ModuleRegistry,
ProcessFileInputParams,
enableDevValidations,
} from "ag-grid-community";
import {
ColumnMenuModule,
ContextMenuModule,
ExcelExportModule,
} from "ag-grid-enterprise";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
const modules = [
ClientSideRowModelModule,
AutoGenerateColumnsModule,
ExcelExportModule,
ColumnMenuModule,
ContextMenuModule,
];
declare let XLSX: any;
const parseWorkbook: (workbook: any) => Record<string, unknown>[] = (
workbook: any,
) => {
const firstSheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[firstSheetName];
return XLSX.utils.sheet_to_json(worksheet);
};
const GridExample = () => {
const gridRef = useRef<AgGridReact>(null);
const containerStyle = useMemo(() => ({ width: "100%", height: "100%" }), []);
const gridStyle = useMemo(() => ({ height: "100%", width: "100%" }), []);
const defaultColDef = useMemo<ColDef>(() => {
return {
minWidth: 80,
flex: 1,
};
}, []);
const processFileInput = useCallback((params: ProcessFileInputParams) => {
const file = params.files[0];
if (!file) return;
const reader = new FileReader();
reader.onerror = () => params.fail("Failed to read file");
reader.onload = (e) => {
try {
const workbook = XLSX.read(
new Uint8Array(e.target?.result as ArrayBuffer),
);
params.success(parseWorkbook(workbook));
} catch {
params.fail("Failed to parse file");
}
};
reader.readAsArrayBuffer(file);
}, []);
const uploadFile = useCallback(() => {
const curr = gridRef.current!.api.getGridOption("activeOverlay");
gridRef.current!.api.setGridOption(
"activeOverlay",
curr === "agFileInputOverlay" ? undefined : "agFileInputOverlay",
);
}, []);
const importExcel = useCallback(() => {
fetch("https://www.ag-grid.com/example-assets/olympic-data.xlsx")
.then((response) => response.arrayBuffer())
.then((data: ArrayBuffer) => {
const workbook = XLSX.read(new Uint8Array(data));
gridRef.current!.api.updateGridOptions({
rowData: parseWorkbook(workbook),
activeOverlay: undefined,
});
});
}, []);
return (
<AgGridProvider modules={modules}>
<div style={containerStyle}>
<div className="example-wrapper">
<div style={{ marginBottom: "1rem" }}>
<button onClick={importExcel}>Load Sample Excel</button>
<button onClick={uploadFile}>Upload File</button>
</div>
<div style={gridStyle}>
<AgGridReact
ref={gridRef}
autoGenerateColumnDefs={true}
defaultColDef={defaultColDef}
processFileInput={processFileInput}
/>
</div>
</div>
</div>
</AgGridProvider>
);
};
const root = createRoot(document.getElementById("root")!);
root.render(
<StrictMode>
<GridExample />
</StrictMode>,
);
.example-wrapper {
display: flex;
flex-direction: column;
height: 100%;
}
#myGrid {
flex: 1 1 0px;
width: 100%;
}