How to Export Selection from Telerik RadGridView to Excel

2 posts, 0 answers
  1. Devid
    Devid avatar
    10 posts
    Member since:
    Aug 2016

    Posted 24 Aug Link to this post

    I am using WPF. I want to Export the selected Rows from the RadGridView to Excel. I have managed to export all the rows to Excel, but if the user makes a selection I want to export only the selected rows to Excel. Here is my code below, I just need ti implement the TODO section. Can someone help ?

     

     

     

    01.public void Execute_OpenExportView(RadGridView gridViewExport)
    02.   {
    03.       if (gridViewExport==null)
    04.       {
    05.           return;
    06.       }
    07. 
    08.       string extension = "xlsx";
    09.       SaveFileDialog dialog = new SaveFileDialog()
    10.       {
    11.           DefaultExt = extension,
    12.           Filter = String.Format("{1} files (*.{0})|*.{0}|All files(*.*)|*.*", extension, "Excel"),
    13.           FilterIndex = 1,
    14.           AddExtension = true,
    15.           Title = "Export to Excel",
    16.           //FileName =
    17.       };
    18. 
    19. 
    20.       //TODO export the selected rows from RadGRidView
    21.       if (gridViewExport.SelectedItems.Count!=0)
    22.       {
    23. 
    24. 
    25.       }
    26. 
    27.       if (dialog.ShowDialog()==true)
    28.       {
    29.           using (Stream stream = dialog.OpenFile())
    30.           {
    31.               gridViewExport.ExportToXlsx(stream,
    32.                   new GridViewDocumentExportOptions()
    33.                   {
    34.                       ShowColumnFooters = true,
    35.                       ShowColumnHeaders = true,
    36.                       ShowGroupFooters = true,
    37.                       AutoFitColumnsWidth = true,
    38.                   });
    39.           }
    40.       }
    41. 
    42.   }

  2. Devid
    Devid avatar
    10 posts
    Member since:
    Aug 2016

    Posted 24 Aug in reply to Devid Link to this post

    Here is the solution:

    01.using (Stream stream = dialog.OpenFile())
    02.  {
    03.        gridViewExport.ExportToXlsx(stream,
    04.            new GridViewDocumentExportOptions()
    05.            {
    06.                Items = gridViewExport.SelectedItems, //this is where the magic happens
    07.                               ShowColumnFooters = true,
    08.                               ShowColumnHeaders = true,
    09.                               ShowGroupFooters = true,
    10.                               AutoFitColumnsWidth = true,
    11.            });
    12.  }

     

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top