Exporting selected rows from Gridview using GridViewSpreadExport

1 Answer 79 Views
GridView
Spiffy Jones
Top achievements
Rank 1
Spiffy Jones asked on 16 Jun 2023, 05:57 PM

Hi,

Is it possible to export to Excel only the selected rows from a gridview using GridViewSpreadExport?

My code looks like this at the moment:

Using ms As New System.IO.MemoryStream

            mspreadExporter = New GridViewSpreadExport(radDocuments) 'radDocuments is the gridview

            Dim exportRenderer As New SpreadExportRenderer()

            mspreadExporter.ExportFormat = SpreadExportFormat.Xlsx
            mspreadExporter.ExportVisualSettings = False
            mspreadExporter.HiddenColumnOption = HiddenOption.DoNotExport
            mspreadExporter.ChildViewExportMode = ChildViewExportMode.ExportCurrentlyActiveView
            mspreadExporter.SheetMaxRows = ExcelMaxRows._1048576
            mspreadExporter.SheetName = "Report"
            mspreadExporter.FileExportMode = FileExportMode.CreateOrOverrideFile
            mspreadExporter.RunExport(ms, exportRenderer)

            Using fileStream As New System.IO.FileStream(strFilename, IO.FileMode.Create, IO.FileAccess.Write)
                ms.WriteTo(fileStream)
            End Using

End Using

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 20 Jun 2023, 11:39 AM

Hi Stéphane Thibadueau

Thank you for the shared code snippet.

The GridViewSpreadExport does not expose a mechanism to export only the selected rows. However, you can achieve this using custom code. The GridViewSpreadExport exposes a HiddenRowOption which determines whether to export hidden rows or not. Using this in hand, we can hide the unselected rows, export the control and show the rows again.

Private Sub radButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim list As List(Of GridViewRowInfo) = New List(Of GridViewRowInfo)()

    For Each rowInfo As GridViewRowInfo In Me.radGridView1.Rows

        If rowInfo.IsSelected = False Then
            rowInfo.IsVisible = False
            list.Add(rowInfo)
        End If
    Next

    Dim spreadExporter As GridViewSpreadExport = New GridViewSpreadExport(Me.radGridView1)
    Dim spreadExportRenderer As ISpreadExportRenderer = New SpreadExportRenderer()
    spreadExporter.HiddenColumnOption = HiddenOption.ExportAlways
    spreadExporter.ExportFormat = SpreadExportFormat.Xlsx
    spreadExporter.HiddenRowOption = HiddenOption.DoNotExport
    spreadExporter.ExportVisualSettings = True
    spreadExporter.RunExport("../../exportedFile.xlsx", spreadExportRenderer, "SheetName")

    For Each rowInfo As GridViewRowInfo In list
        rowInfo.IsVisible = True
    Next
End Sub

Using the above code snippet, you can export only the selected rows.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Spiffy Jones
Top achievements
Rank 1
commented on 20 Jun 2023, 06:35 PM

Thank you!  Nice little use of the hidden rows
Tags
GridView
Asked by
Spiffy Jones
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or