---
title: "Excel Export - Multiple Sheets"
enterprise: true
framework: vue
version: "36.1.0"
---

# Excel Export - Multiple Sheets

Excel Export provides a way to export an Excel file with multiple sheets. This can be useful when you need to export data from different grids into a single Excel file.

## How it works

Exporting the grid into different sheets follows a specific process:

1. You start the process by calling the `getSheetDataForExcel` method on a grid instance to get the data exported for a specific sheet.
2. You call this method multiple times either on the same grid with different data (or different export params) or on different instances of the grid, and you store each exported data set as an element of an Array.
3. Once all the needed sheets have been stored in the Array, call the `exportMultipleSheetsAsExcel` or `getMultipleSheetsAsExcel` methods to package them in a single Excel workbook.

> **Warning**
>
> Calling `getSheetDataForExcel` starts a **Multiple Sheet** export process, that can only be ended by calling the `exportMultipleSheetsAsExcel` or `getMultipleSheetsAsExcel` methods. Until one of these two methods is called to complete the process, no data can be exported from the grid using `exportDataAsExcel` or `getDataAsExcel`.

## Using Selected Rows

In this example, we use the `onlySelected=true` property to segment the grid data into multiple sheets, each containing 100 data rows. Specifically:

1. We manually select 100 rows at a time using `setNodesSelected`.
2. We then use `getSheetDataForExcel` with the `onlySelected` option to generate sheet data for these selected nodes only.
3. We then deselect rows again to avoid affecting the UI.

Note the following:

- The header is exported on each page, so each page will contain 101 records (including the header).
- Because each export did not have a specified `sheetName`, they will be named `ag-grid`, `ag-grid_1`, `ag-grid_2` and so on.

#### Excel Export - Multiple Sheets with Data Selection

```ts
import {
  createApp,
  defineComponent,
  onBeforeMount,
  ref,
  shallowRef,
} from "vue";
import { AgGridVue } from "ag-grid-vue3";
import "./styles.css";
import {
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  IRowNode,
  ModuleRegistry,
  NumberFilterModule,
  RowApiModule,
  RowSelectionModule,
  RowSelectionOptions,
  TextFilterModule,
  enableDevValidations,
} from "ag-grid-community";
import {
  ColumnMenuModule,
  ContextMenuModule,
  ExcelExportModule,
} from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  TextFilterModule,
  NumberFilterModule,
  RowSelectionModule,
  RowApiModule,
  ClientSideRowModelModule,
  ExcelExportModule,
  ColumnMenuModule,
  ContextMenuModule,
]);

const VueExample = defineComponent({
  template: `
        <div style="height: 100%">
                <div class="container">
      <div>
        <button v-on:click="onBtExport()" style="margin-bottom: 5px; font-weight: bold">Export to Excel</button>
      </div>
      <div class="grid-wrapper">
        <ag-grid-vue
          style="width: 100%; height: 100%;"
          @grid-ready="onGridReady"
          :columnDefs="columnDefs"
          :defaultColDef="defaultColDef"
          :rowSelection="rowSelection"
          :rowData="rowData"></ag-grid-vue>
        </div>
      </div>
        </div>
    `,
  components: {
    "ag-grid-vue": AgGridVue,
  },
  setup(props) {
    const gridApi = shallowRef<GridApi<IOlympicData> | null>(null);
    const columnDefs = ref<ColDef[]>([
      { field: "athlete", minWidth: 200 },
      { field: "age" },
      { field: "country", minWidth: 200 },
      { field: "year" },
      { field: "date", minWidth: 150 },
      { field: "sport", minWidth: 150 },
      { field: "gold" },
      { field: "silver" },
    ]);
    const defaultColDef = ref<ColDef>({
      filter: true,
      minWidth: 100,
      flex: 1,
    });
    const rowSelection = ref<RowSelectionOptions | "single" | "multiple">({
      mode: "multiRow",
      checkboxes: false,
      headerCheckbox: false,
    });
    const rowData = ref<IOlympicData[]>(null);

    function onBtExport() {
      const spreadsheets: string[] = [];
      let nodesToExport: IRowNode[] = [];
      gridApi.value!.forEachNode((node, index) => {
        nodesToExport.push(node);
        if (index % 100 === 99) {
          gridApi.value!.setNodesSelected({
            nodes: nodesToExport,
            newValue: true,
          });
          spreadsheets.push(
            gridApi.value!.getSheetDataForExcel({
              onlySelected: true,
            })!,
          );
          gridApi.value!.deselectAll();
          nodesToExport = [];
        }
      });
      // check if the last page was exported
      if (gridApi.value!.getSelectedNodes().length) {
        spreadsheets.push(
          gridApi.value!.getSheetDataForExcel({
            onlySelected: true,
          })!,
        );
        gridApi.value!.deselectAll();
      }
      gridApi.value!.exportMultipleSheetsAsExcel({
        data: spreadsheets,
        fileName: "ag-grid.xlsx",
      });
    }
    const onGridReady = (params: GridReadyEvent) => {
      gridApi.value = params.api;

      const updateData = (data) => (rowData.value = data);

      fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
        .then((resp) => resp.json())
        .then((data) => updateData(data));
    };

    return {
      gridApi,
      columnDefs,
      defaultColDef,
      rowSelection,
      rowData,
      onGridReady,
      onBtExport,
    };
  },
});

const app = createApp(VueExample);
app.mount("#app");
```

[Live example: Excel Export - Multiple Sheets with Data Selection](https://www.ag-grid.com/examples/excel-export-multiple-sheets/excel-export-multiple-sheets-selected/vue3)

## Using Data Filtering

In this example, we filter on the sport column to segment the grid data into multiple sheets, each containing all the data for a specific sport value.

Note the following:

- The exported Excel file will contain one sheet for each sport result.
- Each sheet was exported using the sport name as the name of the sheet.

#### Excel Export - Multiple Sheets with Filtered Data

```ts
import {
  createApp,
  defineComponent,
  onBeforeMount,
  ref,
  shallowRef,
} from "vue";
import { AgGridVue } from "ag-grid-vue3";
import "./styles.css";
import {
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  NumberFilterModule,
  RowApiModule,
  enableDevValidations,
} from "ag-grid-community";
import {
  ColumnMenuModule,
  ContextMenuModule,
  ExcelExportModule,
  SetFilterModule,
} from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  NumberFilterModule,
  RowApiModule,
  ClientSideRowModelModule,
  ExcelExportModule,
  ColumnMenuModule,
  ContextMenuModule,
  SetFilterModule,
]);

const VueExample = defineComponent({
  template: `
        <div style="height: 100%">
                <div class="container">
      <div>
        <button v-on:click="onBtExport()" style="margin-bottom: 5px; font-weight: bold">Export to Excel</button>
      </div>
      <div class="grid-wrapper">
        <ag-grid-vue
          style="width: 100%; height: 100%;"
          @grid-ready="onGridReady"
          :columnDefs="columnDefs"
          :defaultColDef="defaultColDef"
          :rowData="rowData"></ag-grid-vue>
        </div>
      </div>
        </div>
    `,
  components: {
    "ag-grid-vue": AgGridVue,
  },
  setup(props) {
    const gridApi = shallowRef<GridApi<IOlympicData> | null>(null);
    const columnDefs = ref<ColDef[]>([
      { field: "athlete", minWidth: 200 },
      { field: "age" },
      { field: "country", minWidth: 200 },
      { field: "year" },
      { field: "date", minWidth: 150 },
      { field: "sport", minWidth: 150 },
      { field: "gold" },
      { field: "silver" },
    ]);
    const defaultColDef = ref<ColDef>({
      filter: true,
      minWidth: 100,
      flex: 1,
    });
    const rowData = ref<IOlympicData[]>(null);

    function onBtExport() {
      const sports: Record<string, boolean> = {};
      gridApi.value!.forEachNode(function (node) {
        if (!sports[node.data!.sport]) {
          sports[node.data!.sport] = true;
        }
      });
      let spreadsheets: string[] = [];
      const performExport = async () => {
        for (const sport in sports) {
          await gridApi.value!.setColumnFilterModel("sport", {
            values: [sport],
          });
          gridApi.value!.onFilterChanged();
          if (gridApi.value!.getColumnFilterModel("sport") == null) {
            throw new Error("Example error: Filter not applied");
          }
          const sheet = gridApi.value!.getSheetDataForExcel({
            sheetName: sport,
          });
          if (sheet) {
            spreadsheets.push(sheet);
          }
        }
        await gridApi.value!.setColumnFilterModel("sport", null);
        gridApi.value!.onFilterChanged();
        gridApi.value!.exportMultipleSheetsAsExcel({
          data: spreadsheets,
          fileName: "ag-grid.xlsx",
        });
        spreadsheets = [];
      };
      performExport();
    }
    const onGridReady = (params: GridReadyEvent) => {
      gridApi.value = params.api;

      const updateData = (data) => (rowData.value = data);

      fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
        .then((resp) => resp.json())
        .then((data) => updateData(data));
    };

    return {
      gridApi,
      columnDefs,
      defaultColDef,
      rowData,
      onGridReady,
      onBtExport,
    };
  },
});

const app = createApp(VueExample);
app.mount("#app");
```

[Live example: Excel Export - Multiple Sheets with Filtered Data](https://www.ag-grid.com/examples/excel-export-multiple-sheets/excel-export-multiple-sheets-by-filter/vue3)

## Multiple Grids to Multiple Sheets

In this example, we export two grids, each into a separate sheet of the same Excel file. Drag a few rows from the grid on the left into the grid on the right and click the export button above the grid.

Note the following:

- The contents of the `Athletes` grid will be exported to the `Athletes` sheet.
- The contents of the `Selected Athletes` grid will be exported to the `Selected Athletes` sheet.
- Only the `onExcelExport` method is relevant to **Excel Export**

#### Excel Export - Multiple Sheets with Multiple Grids

```ts
import { createApp, defineComponent } from "vue";

import type {
  ColDef,
  GetRowIdParams,
  GridApi,
  GridReadyEvent,
  ICellRendererParams,
  RowSelectionOptions,
} from "ag-grid-community";
import {
  ClientSideRowModelApiModule,
  ClientSideRowModelModule,
  CsvExportModule,
  ModuleRegistry,
  RowDragModule,
  RowSelectionModule,
  TextFilterModule,
  enableDevValidations,
} from "ag-grid-community";
import {
  ExcelExportModule,
  exportMultipleSheetsAsExcel,
} from "ag-grid-enterprise";
import { AgGridVue } from "ag-grid-vue3";

import "./styles.css";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  RowDragModule,
  ClientSideRowModelApiModule,
  TextFilterModule,
  RowSelectionModule,
  CsvExportModule,
  ExcelExportModule,
]);

const SportRenderer = defineComponent({
  template: `<i class="far fa-trash-alt" style="cursor: pointer" @click="applyTransaction()"></i>`,
  methods: {
    applyTransaction() {
      this.params.api.applyTransaction({ remove: [this.params.node.data] });
    },
  },
});

const VueExample = defineComponent({
  template: /* html */ `
        <div class="top-container">
            <div>
                <button type="button" class="btn btn-default excel" @click="onExcelExport()">
                    <i class="far fa-file-excel" style="margin-right: 5px; color: green;"></i>Export to Excel
                </button>
                <button type="button" class="btn btn-default reset" @click="reset()">
                    <i class="fas fa-redo" style="margin-right: 5px;"></i>Reset
                </button>
            </div>
            <div class="grid-wrapper">
                <div class="panel panel-primary" style="margin-right: 10px;">
                    <div class="panel-heading">Athletes</div>
                    <div class="panel-body">
                        <ag-grid-vue
                                id="eLeftGrid"
                                style="height: 100%;"
                                :defaultColDef="defaultColDef"
                                :rowSelection="rowSelection"
                                :rowDragMultiRow="true"
                                :getRowId="getRowId"
                                :rowDragManaged="true"
                                :suppressMoveWhenRowDragging="true"
                                :rowData="leftRowData"
                                :columnDefs="leftColumns"
                                @grid-ready="onGridReady($event, 0)"
                                >
                        </ag-grid-vue>
                    </div>
                </div>
                <div class="panel panel-primary" style="margin-left: 10px;">
                    <div class="panel-heading">Selected Athletes</div>
                    <div class="panel-body">
                        <ag-grid-vue
                                id="eRightGrid"
                                style="height: 100%;"
                                :defaultColDef="defaultColDef"
                                :getRowId="getRowId"
                                :rowDragManaged="true"
                                :rowData="rightRowData"
                                :columnDefs="rightColumns"
                                @grid-ready="onGridReady($event, 1)"
                                >
                        </ag-grid-vue>
                    </div>
                </div>
            </div>
        </div>`,
  components: {
    "ag-grid-vue": AgGridVue,
    SportRenderer,
  },
  data: function () {
    return {
      leftRowData: null,
      rightRowData: null,
      leftApi: null,
      rightApi: null,
      rowSelection: <RowSelectionOptions>{
        mode: "multiRow",
      },
      defaultColDef: <ColDef>{
        flex: 1,
        minWidth: 100,
        filter: true,
      },
      leftColumns: <ColDef[]>[
        {
          rowDrag: true,
          maxWidth: 50,
          suppressHeaderMenuButton: true,
          suppressHeaderFilterButton: true,
          rowDragText: (params, dragItemCount) => {
            if (dragItemCount > 1) {
              return dragItemCount + " athletes";
            }
            return params.rowNode.data.athlete;
          },
        },
        { field: "athlete" },
        { field: "sport" },
      ],
      rightColumns: <ColDef[]>[
        {
          rowDrag: true,
          maxWidth: 50,
          suppressHeaderMenuButton: true,
          suppressHeaderFilterButton: true,
          rowDragText: (params, dragItemCount) => {
            if (dragItemCount > 1) {
              return dragItemCount + " athletes";
            }
            return params.rowNode.data.athlete;
          },
        },
        { field: "athlete" },
        { field: "sport" },
        {
          suppressHeaderMenuButton: true,
          suppressHeaderFilterButton: true,
          maxWidth: 50,
          cellRenderer: "SportRenderer",
        },
      ],
    };
  },
  beforeMount() {
    fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
      .then((resp) => resp.json())
      .then((data) => {
        const athletes = [];
        let i = 0;

        while (athletes.length < 20 && i < data.length) {
          const pos = i++;
          if (athletes.some((rec) => rec.athlete === data[pos].athlete)) {
            continue;
          }
          athletes.push(data[pos]);
        }
        this.rawData = athletes;
        this.loadGrids();
      });
  },
  methods: {
    getRowId(params) {
      return params.data.athlete;
    },

    loadGrids() {
      this.leftRowData = [...this.rawData.slice(0, this.rawData.length / 2)];
      this.rightRowData = [...this.rawData.slice(this.rawData.length / 2)];
    },

    reset() {
      this.loadGrids();
    },

    onGridReady(params: GridReadyEvent, side: number) {
      if (side === 0) {
        this.leftApi = params.api;
      }

      if (side === 1) {
        this.rightApi = params.api;
        this.addGridDropZone();
      }
    },

    addGridDropZone() {
      const dropZoneParams = this.rightApi.getRowDropZoneParams({
        onDragStop: (params) => {
          const nodes = params.nodes;

          this.leftApi.applyTransaction({
            remove: nodes.map(function (node) {
              return node.data;
            }),
          });
        },
      });

      this.leftApi.addRowDropZone(dropZoneParams);
    },

    onExcelExport() {
      const spreadsheets = [];

      spreadsheets.push(
        this.leftApi.getSheetDataForExcel({ sheetName: "Athletes" }),
        this.rightApi.getSheetDataForExcel({ sheetName: "Selected Athletes" }),
      );

      exportMultipleSheetsAsExcel({
        data: spreadsheets,
        fileName: "ag-grid.xlsx",
      });
    },
  },
});

createApp(VueExample).mount("#app");
```

[Live example: Excel Export - Multiple Sheets with Multiple Grids](https://www.ag-grid.com/examples/excel-export-multiple-sheets/excel-export-multiple-sheets-multiple-grids/vue3)

## API

### API Methods

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `getSheetDataForExcel` | `Function` |  |  | This is method to be used to get the grid's data as a sheet, that will later be exported either by `getMultipleSheetsAsExcel()` or `exportMultipleSheetsAsExcel()`. Module: [`ExcelExportModule`](https://www.ag-grid.com/vue-data-grid/modules/). |
| `exportMultipleSheetsAsExcel` | `Function` |  |  | Downloads an Excel export of multiple sheets in one file. Module: [`ExcelExportModule`](https://www.ag-grid.com/vue-data-grid/modules/). |
| `getMultipleSheetsAsExcel` | `Function` |  |  | Similar to `exportMultipleSheetsAsExcel`, except instead of downloading a file, it will return a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) to be processed by the user. Module: [`ExcelExportModule`](https://www.ag-grid.com/vue-data-grid/modules/). |
