How to export groups to Excel and Outlining from WPF SfDataGrid?
In SfDataGrid control, you can export data to Excel by using the ExportToExcel method that has ExcelExportingOptions as an argument. ExcelExportingOptions provides various properties to customize the exporting. You can expand or collapse the exported group in Excel by setting the AllowOutlining property to True in ExcelExportingOptions. The default value of this property is True, so you can expand or collapse the exported group in Excel until you explicitly set this property as False.
C#
using Syncfusion.UI.Xaml.Grid.Converter; using Syncfusion.XlsIO; private static void OnExecuteExportToExcel(object sender, ExecutedRoutedEventArgs args) { var dataGrid = args.Source as SfDataGrid; if (dataGrid == null) return; try { // Creating an instance for ExcelExportingOptions which is passed as a parameter to the ExportToExcel method. ExcelExportingOptions options = new ExcelExportingOptions(); options.AllowOutlining = true; // Exports Datagrid to Excel and returns ExcelEngine. var excelEngine = dataGrid.ExportToExcel(dataGrid.View, options); // Gets the exported workbook from the ExcelEngine. var workBook = excelEngine.Excel.Workbooks[0]; // Saving the workbook. SaveFileDialog sfd = new SaveFileDialog { FilterIndex = 2, Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx" }; if (sfd.ShowDialog() == true) { using (Stream stream = sfd.OpenFile()) { if (sfd.FilterIndex == 1) workBook.Version = ExcelVersion.Excel97to2003; else workBook.Version = ExcelVersion.Excel2010; workBook.SaveAs(stream); } //Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer] System.Diagnostics.Process.Start(sfd.FileName); } } }
The following snapshot illustrates the result of the above code in SfDataGrid.
Expanded and Collapsed group in Grid:
Expanded and Collapsed group in the Exported Excel:
Sample Links:
Conclusion
I hope you enjoyed learning about how to export groups to Excel and Outlining from WPF SfDataGrid.
You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!