如何从Telerik RadGridView导出选区到Excel

本文关键字:选区 Excel RadGridView Telerik | 更新日期: 2023-09-27 18:03:42

我只想将RadGridView的选定行导出到Excel。我已经成功地导出了一切,但是如果用户选择了行,我只想从RadGridView导出这些行到Excel。下面是我的代码:(Todo应该被实现)

 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,
                    });
            }
        }
    }

如何从Telerik RadGridView导出选区到Excel

您可以使用GridViewDocumentExportOptions的内置Items属性,并将RadGridView的SelectedItems集合传递给它。有关更多信息,请查看Telerik UI for WPF文档中的ExportFormat.Xlsx主题。

这将是你代码中的解决方案:

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