Exporting DataGrid to Excel Made Easy in .NET MAUI

Exporting DataGrid to Excel Made Easy in .NET MAUI

Exporting DataGrid to Excel Made Easy in .NET MAUI

TLDR: Learn to export your grid data to an Excel document using the Syncfusion .NET MAUI DataGrid control with custom export options like excluding columns, exporting selected rows, and applying styles.

Syncfusion .[NET MAUI DataGrid](syncfusion.com/maui-controls/maui-datagrid ".NET MAUI DataGrid") control is used to display and manipulate data in a tabular view. Its rich feature set includes different column types, sorting, autofit columns and rows, and styling all elements.

In this blog, we’ll guide you through the steps to export the .NET MAUI DataGrid to an Excel file with custom options!

Note: Please refer to the [.NET MAUI DataGrid getting started documentation](help.syncfusion.com/maui/datagrid/getting-s.. "Getting started with the .NET MAUI DataGrid") before proceeding.

Export DataGrid to Excel

The [Syncfusion.Maui.DataGridExport](nuget.org/packages/Syncfusion.Maui.DataGrid.. "Syncfusion.Maui.DataGridExport NuGet package") NuGet package provides the required Excel exporting functionalities. Here, we will use our [Syncfusion.XlsIO.NET](https://www.nuget.org/packages/Syncfusion.XlsIO.NET/ "Syncfusion.XlsIO.NET NuGet package") packages to export the DataGrid to Excel.

To do so, include the Syncfusion.Maui.DataGridExport package in your application.

Then, add the .NET MAUI DataGrid control and an export button to your XAML page. Refer to the following code example.

All the Excel exporting methods are available in the [DataGridExcelExportingController](help.syncfusion.com/cr/maui/Syncfusion.Maui.. "DataGridExcelExportingController class of the .NET MAUI DataGrid"). Using the [ExportToExcel](help.syncfusion.com/cr/maui/Syncfusion.Maui..SfDataGrid "ExportToExcel method in the .NET MAUI DataGrid") method, you can export the DataGrid content to an Excel file and save it as a workbook.

Refer to the following code example.

private void ExportToExcel_Clicked(object sender, EventArgs e) { DataGridExcelExportingController excelExport = new DataGridExcelExportingController(); var excelEngine = excelExport.ExportToExcel(this.dataGrid); var workbook = excelEngine.Excel.Workbooks[0]; MemoryStream stream = new MemoryStream(); workbook.SaveAs(stream); workbook.Close(); excelEngine.Dispose();

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

To save the worksheet and view it at a specific location on your machine, you can use the [SaveService](help.syncfusion.com/maui/datagrid/export-to.. "SaveService class of the .NET MAUI DataGrid") class.

Exporting .NET MAUI DataGrid data to an Excel worksheet

Exporting .NET MAUI DataGrid data to an Excel worksheet

Excel exporting with customization

You can also customize the exported Excel sheet using the [DataGridExcelExportingOption](help.syncfusion.com/cr/maui/Syncfusion.Maui.. "DataGridExcelExportingOption class of the .NET MAUI DataGrid").

Export without header

The .NET MAUI DataGrid provides options to export grid data to Excel with or without a header. By default, the header will be included in the exported sheet.

Refer to the following code example.

private void ExportToExcel_Clicked(object sender, EventArgs e) { DataGridExcelExportingController excelExport = new DataGridExcelExportingController(); DataGridExcelExportingOption options = new DataGridExcelExportingOption();
options.CanExportHeader = false; var excelEngine = excelExport.ExportToExcel(this.dataGrid, options); var workbook = excelEngine.Excel.Workbooks[0]; MemoryStream stream = new MemoryStream(); workbook.SaveAs(stream); workbook.Close(); excelEngine.Dispose(); string OutputFilename = "DefaultDataGrid.xlsx"; SaveService saveService = new(); saveService.SaveAndView(OutputFilename, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", stream); }

Exporting grid data without headers to an Excel document

Exporting grid data without headers to an Excel document

Exclude the columns in the exported Excel

You can exclude specific data columns in a grid from being exported to an Excel sheet. By default, all columns, including hidden ones, are exported.

Refer to the following code example.

private void ExportToExcel_Clicked(object sender, EventArgs e) { DataGridExcelExportingController excelExport = new DataGridExcelExportingController(); DataGridExcelExportingOption options = new DataGridExcelExportingOption(); var list = new List(); list.Add("OrderID"); list.Add("CustomerID"); options.ExcludedColumns = list;
var excelEngine = excelExport.ExportToExcel(this.dataGrid, options); var workbook = excelEngine.Excel.Workbooks[0]; MemoryStream stream = new MemoryStream(); workbook.SaveAs(stream); workbook.Close(); excelEngine.Dispose(); string OutputFilename = "DefaultDataGrid.xlsx"; SaveService saveService = new(); saveService.SaveAndView(OutputFilename, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", stream); }

Exporting grid data excluding specific columns to an Excel file

Exporting grid data excluding specific columns to an Excel file

Export the selected rows

Let’s see how to export only the selected rows in a DataGrid by utilizing the ExportToExcel method and passing instances of the SelectedRows collection.

Refer to the following code example.

private void ExportToExcel_Clicked(object sender, EventArgs e) { DataGridExcelExportingController excelExport = new DataGridExcelExportingController(); ObservableCollection selectedItems = dataGrid.SelectedRows; var excelEngine = excelExport.ExportToExcel(this.dataGrid, selectedItems); var workbook = excelEngine.Excel.Workbooks[0]; MemoryStream stream = new MemoryStream(); workbook.SaveAs(stream); workbook.Close(); excelEngine.Dispose(); string OutputFilename = "DefaultDataGrid.xlsx"; SaveService saveService = new(); saveService.SaveAndView(OutputFilename, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", stream); }

Exporting selected rows in a grid to an Excel sheet

Exporting selected rows in a grid to an Excel sheet

Customize the starting row and column in an Excel sheet

In the exporting option, you can specify the start column using the [DataGridExcelExportingOption.StartColumnIndex](help.syncfusion.com/cr/maui/Syncfusion.Maui.. "StartColumnIndex property of the .NET MAUI DataGrid") and designate the start row using the [DataGridExcelExportingOption.StartRowIndex](help.syncfusion.com/cr/maui/Syncfusion.Maui.. "StartRowIndex property of the .NET MAUI DataGrid").

Refer to the following code example.

private void ExportToExcel_Clicked(object sender, EventArgs e) { DataGridExcelExportingController excelExport = new DataGridExcelExportingController(); DataGridExcelExportingOption options = new DataGridExcelExportingOption(); options.StartRowIndex = 4; options.StartColumnIndex = 2; var excelEngine = excelExport.ExportToExcel(this.dataGrid, options); var workbook = excelEngine.Excel.Workbooks[0]; MemoryStream stream = new MemoryStream(); workbook.SaveAs(stream); workbook.Close(); excelEngine.Dispose(); string OutputFilename = "DefaultDataGrid.xlsx"; SaveService saveService = new(); saveService.SaveAndView(OutputFilename, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", stream); }

Customizing the starting row and column in the exported data in an Excel sheet

Customizing the starting row and column in the exported data in an Excel sheet

Apply styles for the exported sheet

You can also apply styles for the Excel sheet by utilizing the [RowExporting](help.syncfusion.com/cr/maui/Syncfusion.Maui.. "RowExporting property of the .NET MAUI DataGrid") and [CellExporting](help.syncfusion.com/cr/maui/Syncfusion.Maui.. "CellExporting property of the .NET MAUI DataGrid") events within the DataGridExcelExportingController.

Refer to the following code example.

private void ExportToExcel_Clicked(object sender, EventArgs e) { DataGridExcelExportingController excelExport = new DataGridExcelExportingController(); DataGridExcelExportingOption options = new DataGridExcelExportingOption(); excelExport.RowExporting += ExcelExport_RowExporting; var excelEngine = excelExport.ExportToExcel(this.dataGrid, options); var workbook = excelEngine.Excel.Workbooks[0]; MemoryStream stream = new MemoryStream(); workbook.SaveAs(stream); workbook.Close(); excelEngine.Dispose(); string OutputFilename = "DefaultDataGrid.xlsx"; SaveService saveService = new(); saveService.SaveAndView(OutputFilename, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", stream); }

private void ExcelExport_RowExporting(object? sender, DataGridRowExcelExportingEventArgs e) { if (!(e.Record.Data is OrderInfo)) return; if (e.RowType == ExportRowType.RecordRow) e.Range.CellStyle.ColorIndex = Syncfusion.XlsIO.ExcelKnownColors.Aqua; }

Applying styles to the exported grid data in an Excel sheet

Applying styles to the exported grid data in an Excel sheet

Reference

For more details, refer to [Export .NET MAUI DataGrid to Excel documentation](help.syncfusion.com/maui/datagrid/export-to.. "Export .NET MAUI DataGrid to Excel documentation").

[

Syncfusion Ad

Supercharge your cross-platform apps with Syncfusion's robust .NET MAUI controls.

Try It Free

](syncfusion.com/downloads/maui)

Conclusion

Thanks for reading! This blog offers an extensive guide on efficiently exporting [DataGrid](syncfusion.com/maui-controls/maui-datagrid ".NET MAUI DataGrid") to Excel in .NET MAUI, equipping developers with essential tools for a smooth process. We believe that the steps outlined here have been valuable and enlightening.

If you’re interested in delving deeper into .NET MAUI, you can easily access and evaluate it by downloading [Essential Studio for .NET MAUI](syncfusion.com/downloads/maui "Get free evaluation of the Essential Studio for .NET MAUI") for free. Our esteemed customers can acquire the latest version of Essential Studio from the [License and Downloads](syncfusion.com/account/downloads "Essential Studio License and Downloads page") page.

Should you need any assistance or have further inquiries, contact us via our [support forum](syncfusion.com/forums "Syncfusion Support Forum"), [support portal](support.syncfusion.com "Syncfusion Support Portal"), or [feedback portal](syncfusion.com/feedback "Syncfusion Feedback Portal"). We are committed to providing help and support in every possible way.

Test Flight

App Center Badge

Google Play Store Badge

Microsoft Badge

Github Store Badge