Articles in this section
Category / Section

How to export groups to Excel and also export group Outlining from SfDataGrid?

4 mins read

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:

D:\Syncfusion\Issues\2014 Volume 2\KB Issues\KB Images\ExportGrid.PNG

Expanded and Collapsed group in the Exported Excel:

 D:\Syncfusion\Issues\2014 Volume 2\KB Issues\KB Images\ExportExcel.PNG

Sample Links:

WPF

WRT

UWP

 

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