Wednesday, April 13, 2016

Export Telerik Windows GridView Control Data to Excel File

Here is some code snippet which will generate excel file of the telerik windows grid view. I assuming that you already bind data to your telerik grid on your windows application form and performing export to excel on any event.

- Declare new save file dialog in which you will enter your file name
Dim saveFileDialog As New System.Windows.Forms.SaveFileDialog()
- Below code snippet filters your file to be saved for xls only
saveFileDialog.Filter = "Excel (*.xls)|*.xls"
If saveFileDialog.ShowDialog() <> DialogResult.OK Then
    Return
End If
- Below code snippet checks whether file name has been entered or not
If saveFileDialog.FileName.Equals([String].Empty) Then
    RadMessageBox.SetThemeName(MyBase.ThemeName)
    RadMessageBox.Show("Please enter a file name.")
    Return
End If
- Below code snippet acceps name you entered and some default entries to generate your file
Dim fileName As String = saveFileDialog.FileName
Dim excelExporter As New ExportToExcelML("GridName")
excelExporter.SummariesExportOption = SummariesOption.ExportAll
excelExporter.SheetMaxRows = ExcelMaxRows._1048576
excelExporter.RunExport(fileName)
RadMessageBox.SetThemeName(MyBase.ThemeName)
- Below code snippet asks dialog to open saved file 
Dim dr As DialogResult = RadMessageBox.Show("The data in the grid was exported successfully. Do you want to open the file?", "Export to Excel", MessageBoxButtons.YesNo, RadMessageIcon.Question)
If dr = DialogResult.Yes Then
    System.Diagnostics.Process.Start(fileName)
End If

This way you can export your telerik windows control grid view data to excel file in default format.

No comments:

Post a Comment