Exporting DataGrid to Excel Made Easy in .NET MAUI
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
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 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 before proceeding.

Export DataGrid to Excel

The Syncfusion.Maui.DataGridExport NuGet package provides the required Excel exporting functionalities. Here, we will use our Syncfusion.XlsIO.NET 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.

<StackLayout>
 <HorizontalStackLayout Spacing="10">
  <Button Text="Export To Excel" Margin="10,10,10,5"
          Clicked="ExportToExcel_Clicked" />
 </HorizontalStackLayout>
 <syncfusion:SfDataGrid x:Name="dataGrid"
                        Margin="20"
                        VerticalOptions="FillAndExpand"
                        ItemsSource="{Binding OrderInfoCollection}"
                        GridLinesVisibility="Both"
                        HeaderGridLinesVisibility="Both"
                        AutoGenerateColumnsMode="None"
                        ColumnWidthMode="Auto">
  <syncfusion:SfDataGrid.Columns>
   <syncfusion:DataGridNumericColumn Format="D"
                                     HeaderText="Order ID"
                                     MappingName="OrderID">
   </syncfusion:DataGridNumericColumn>
   <syncfusion:DataGridTextColumn HeaderText="Customer ID"
                                  MappingName="CustomerID">
   </syncfusion:DataGridTextColumn>
   <syncfusion:DataGridTextColumn MappingName="Customer"
                                  HeaderText="Customer">
   </syncfusion:DataGridTextColumn>
   <syncfusion:DataGridTextColumn HeaderText="Ship City"
                                  MappingName="ShipCity">
   </syncfusion:DataGridTextColumn>
   <syncfusion:DataGridTextColumn HeaderText="Ship Country"
                                  MappingName="ShipCountry">
   </syncfusion:DataGridTextColumn>
  </syncfusion:SfDataGrid.Columns>
 </syncfusion:SfDataGrid>
</StackLayout>

All the Excel exporting methods are available in the DataGridExcelExportingController. Using the ExportToExcel 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 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

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<string>();
    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<object> 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 and designate the start row using the DataGridExcelExportingOption.StartRowIndex.

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 and CellExporting 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 and GitHub demo.

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

Conclusion

Thanks for reading! This blog offers an extensive guide on efficiently exporting 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 for free. Our esteemed customers can acquire the latest version of Essential Studio from the License and Downloads page.

Should you need any assistance or have further inquiries, contact us via our support forumsupport portal, or 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

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed