Articles in this section

How to export multiple Datagrid in an Excel file in .NET MAUI DataGrid?

The .NET MAUI DataGrid supports exporting multiple DataGrids into a single Excel file.

XAML
We have a Tab View; it has three tab items. Each item consists of one datagrid. We can export the datagrid using the button click.

<StackLayout>
    <Button Text="Export to Excel" Clicked="ExportToExcel_Clicked"/>
    <tabView:SfTabView x:Name="tabView">
    <tabView:SfTabView.Items>
        <tabView:SfTabItem Header="Employees">
            <tabView:SfTabItem.Content>
                <syncfusion:SfDataGrid x:Name="employeeDataGrid"
                                        ColumnWidthMode="Auto"
                                        GridLinesVisibility="Both"
                                        HeaderGridLinesVisibility="Both"
                                        x:DataType="viewModel:MainViewModel"
                                        ItemsSource="{Binding Employees}">                    
                </syncfusion:SfDataGrid>
            </tabView:SfTabItem.Content>
        </tabView:SfTabItem>

        <tabView:SfTabItem Header="Orders">
            <tabView:SfTabItem.Content>
                <syncfusion:SfDataGrid x:Name="ordersDataGrid"
                                        ColumnWidthMode="Auto"
                                        GridLinesVisibility="Both"
                                        HeaderGridLinesVisibility="Both"
                                        x:DataType="viewModel:MainViewModel"     
                                        ItemsSource="{Binding OrderInfoCollection}">                    
                </syncfusion:SfDataGrid>
            </tabView:SfTabItem.Content>
        </tabView:SfTabItem>

        <tabView:SfTabItem Header="New Employees">
            <tabView:SfTabItem.Content>
                <syncfusion:SfDataGrid x:Name="newEmployeeDataGrid"
                                        GridLinesVisibility="Both"
                                        HeaderGridLinesVisibility="Both"
                                        ColumnWidthMode="Auto"
                                        x:DataType="viewModel:MainViewModel"
                                        ItemsSource="{Binding EmployeeCollection}">  
                </syncfusion:SfDataGrid>
            </tabView:SfTabItem.Content>
        </tabView:SfTabItem>
    </tabView:SfTabView.Items>
</tabView:SfTabView>
</StackLayout>

C#
The DataGridExcelExportingController is responsible for exporting the DataGrid content, and the DataGridExcelExportingOption allows for configuring the export behavior. ExcelEngine is initialized by exporting the employeeDataGrid. A new workbook with three worksheets has been created. Then we export the data from the three different DataGrids (employeeDataGrid, ordersDataGrid, and newEmployeeDataGrid) into the corresponding worksheets in the workbook using the exporting options.

private void ExportToExcel_Clicked(object sender, EventArgs e)
{
    DataGridExcelExportingController excelExport = new DataGridExcelExportingController();
    DataGridExcelExportingOption employeeDataGridOption = new DataGridExcelExportingOption();
    DataGridExcelExportingOption ordersDataGridOption = new DataGridExcelExportingOption();
    DataGridExcelExportingOption newEmployeeDataGridOption = new DataGridExcelExportingOption();

    ExcelEngine newengine = excelExport.ExportToExcel(employeeDataGrid);
    var Newworkbook = newengine.Excel.Workbooks.Create(3);

    var Newworksheet1 = Newworkbook.Worksheets[0];
    var Newworksheet2 = Newworkbook.Worksheets[1];
    var Newworksheet3 = Newworkbook.Worksheets[2];
    Newworksheet1 = excelExport.ExportToExcel(employeeDataGrid, employeeDataGrid.View!, employeeDataGridOption, Newworksheet1);
    Newworksheet2 = excelExport.ExportToExcel(ordersDataGrid, ordersDataGrid.View!, ordersDataGridOption, Newworksheet2);
    Newworksheet3 = excelExport.ExportToExcel(newEmployeeDataGrid, newEmployeeDataGrid.View!, newEmployeeDataGridOption, Newworksheet3);

    MemoryStream stream = new MemoryStream();

    Newworkbook.SaveAs(stream);
    Newworkbook.Close();
    newengine.Dispose();

    string OutputFilename = "DataGrid.xlsx";
    SaveService saveService = new();
    saveService.SaveAndView(OutputFilename, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", stream);
}

Download the complete sample from GitHub

Conclusion

I hope you enjoyed learning how to export multiple DataGrids into an Excel file in the MAUI DataGrid.

You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.

For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI DataGrid and other .NET MAUI components.

Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied