This is a migrated thread and some comments may be shown as answers.

How to Export Selection from Telerik RadGridView to Excel

1 Answer 125 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Devid
Top achievements
Rank 1
Devid asked on 24 Aug 2016, 01:09 PM

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.   }

1 Answer, 1 is accepted

Sort by
0
Devid
Top achievements
Rank 1
answered on 24 Aug 2016, 02:15 PM

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.  }

 

Tags
GridView
Asked by
Devid
Top achievements
Rank 1
Answers by
Devid
Top achievements
Rank 1
Share this question
or