-
+
+
+
+
diff --git a/documentation/ag-grid-docs/src/content/docs/excel-import/_examples/excel-import/main.ts b/documentation/ag-grid-docs/src/content/docs/excel-import/_examples/excel-import/main.ts
index 7de9b8acebd..f687d7a910c 100644
--- a/documentation/ag-grid-docs/src/content/docs/excel-import/_examples/excel-import/main.ts
+++ b/documentation/ag-grid-docs/src/content/docs/excel-import/_examples/excel-import/main.ts
@@ -1,11 +1,18 @@
-import type { GridApi, GridOptions } from 'ag-grid-community';
-import { ClientSideRowModelModule, ModuleRegistry, ValidationModule, createGrid } from 'ag-grid-community';
+import type { GridApi, GridOptions, ProcessFileInputParams } from 'ag-grid-community';
+import {
+ AutoGenerateColumnsModule,
+ ClientSideRowModelModule,
+ ModuleRegistry,
+ ValidationModule,
+ createGrid,
+} from 'ag-grid-community';
import { ColumnMenuModule, ContextMenuModule, ExcelExportModule } from 'ag-grid-enterprise';
declare let XLSX: any;
ModuleRegistry.registerModules([
ClientSideRowModelModule,
+ AutoGenerateColumnsModule,
ExcelExportModule,
ColumnMenuModule,
ContextMenuModule,
@@ -15,99 +22,51 @@ ModuleRegistry.registerModules([
let gridApi: GridApi;
const gridOptions: GridOptions = {
- columnDefs: [
- { field: 'athlete', minWidth: 180 },
- { field: 'age' },
- { field: 'country', minWidth: 150 },
- { field: 'year' },
- { field: 'date', minWidth: 130 },
- { field: 'sport', minWidth: 100 },
- { field: 'gold' },
- { field: 'silver' },
- { field: 'bronze' },
- { field: 'total' },
- ],
+ autoGenerateColumnDefs: true,
defaultColDef: {
minWidth: 80,
flex: 1,
},
- rowData: [],
+ processFileInput: (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);
+ },
};
-// read the raw data and convert it to a XLSX workbook
-function convertDataToWorkbook(dataRows: ArrayBuffer) {
- /* convert data to binary string */
- const data = new Uint8Array(dataRows);
- const arr = [];
-
- for (let i = 0; i !== data.length; ++i) {
- arr[i] = String.fromCharCode(data[i]);
- }
-
- const bstr = arr.join('');
-
- return XLSX.read(bstr, { type: 'binary' });
-}
-
-// pull out the values we're after, converting it into an array of rowData
-
-function populateGrid(api: GridApi, workbook: any) {
- // our data is in the first sheet
+function parseWorkbook(workbook: any): Record