Devid
Devid

Reputation: 1983

How to Export Selection from Telerik RadGridView to Excel

I want to export only the selected rows of my RadGridView to Excel. I have managed to export everything, but If the user has selected rows I want only to export those rows from the RadGridView to Excel. Here is my code: (The Todo should be implemented)

 public void Execute_OpenExportView(RadGridView gridViewExport)
    {
        if (gridViewExport==null)
        {
            return;
        }

        string extension = "xlsx";
        SaveFileDialog dialog = new SaveFileDialog()
        {
            DefaultExt = extension,
            Filter = String.Format("{1} files (*.{0})|*.{0}|All files(*.*)|*.*", extension, "Excel"),
            FilterIndex = 1,
            AddExtension = true,
            Title = "Export to Excel",
            //FileName = 
        };


        //TODO export the selected rows from RadGRidView
        if (gridViewExport.SelectedItems.Count!=0)
        {


        }

        if (dialog.ShowDialog()==true)
        {
            using (Stream stream = dialog.OpenFile())
            {
                gridViewExport.ExportToXlsx(stream,
                    new GridViewDocumentExportOptions()
                    {
                        ShowColumnFooters = true,
                        ShowColumnHeaders = true,
                        ShowGroupFooters = true,
                        AutoFitColumnsWidth = true,
                    });
            }
        }

    }

Upvotes: 0

Views: 1905

Answers (1)

checho
checho

Reputation: 3120

You can use the built-in Items property of the GridViewDocumentExportOptions and pass the SelectedItems collection of RadGridView to it. Please checkout the ExportFormat.Xlsx topic in the Telerik UI for WPF documentation for more information.

This would be the solution in your code:

 if (dialog.ShowDialog())
        {
            using (Stream stream = dialog.OpenFile())
            {
                gridViewExport.ExportToXlsx(stream,
                    new GridViewDocumentExportOptions()
                    {
                        Items = gridViewExport.SelectedItems,
                        ShowColumnFooters = true,
                        ShowColumnHeaders = true,
                        ShowGroupFooters = true,
                        AutoFitColumnsWidth = true,
                    });
            }
        }

Upvotes: 1

Related Questions