'Any possible way for export two ag-grids data to a single excel file?

I have two ag-grids one beneath the other in the same page. Using the ag-grid export option, I can export them separately. But I am looking for any possible way to export both to a single Excel file.

BTW both grids has same number of columns and headings. Unfortunately the requirement is to have two different grid.



Solution 1:[1]

It is possible to export the data of two different grids in two different sheets in the same Excel file.

You can extract the content of a single Excel sheet from a grid using the getSheetDataForExcel function of the GridApi object of the grid options.

Using this function, you can create an array of sheets from different grids, that you can only export using the exportMultipleSheetsAsExcel method of the GridApi object of one of the grids, specifying the data property of the configuration object in input.

For instance, let's say that you have two grid options objets for the two different grids, gridOptions1 and gridOptions2. One write a function that handles the Excel export including something like this:

const spreadsheets = [
    gridOptions1.api.getSheetDataForExcel(),
    gridOptions2.api.getSheetDataForExcel()
];

gridOptions1.api.exportMultipleSheetsAsExcel({
    data: spreadsheets,
});

You can even tweak the sheets or file name by passing a configuration object to the grid api method.

Let's say for instance that the exported file name should be CombinedSpreadSheets.xlsx and the two different sheets should be named FirstGridSheet and SecondGridSheet respectively; one can modify the previous code to specify this:

const spreadsheets = [
    gridOptions1.api.getSheetDataForExcel({
        sheetName: "FirstGridSheet",
    }),
    gridOptions2.api.getSheetDataForExcel({
        sheetName: "SecondGridSheet",
    })
];

gridOptions1.api.exportMultipleSheetsAsExcel({
    data: spreadsheets,
    fileName: "CombinedSpreadSheets"
});

The complete guide to create more than one sheets in the same Excel file can be found here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Massimiliano