Export
If you need to export RadGridView`s content, its Export method comes to help. It was introduced in Q1 2010 and it is the preferred method of exporting data. Furthermore, it gives you control over which elements are included in the exported data.
The method expects two parameters:
Stream - usually the file stream which you are exporting data to.
GridViewExportOptions or GridViewCsvExportOptions - used to set the following export options:
Format - the possible formats are defined in the ExportFormat enumeration: Csv, ExcelML, Html or Text
Encoding - the possible values are Encoding.Unicode, Encoding.UTF8, etc.
ShowColumnHeaders - determines whether to export the column headers
ShowColumnFooters - determines whether to export the column footers
ShowGroupFooters - determines whether to export the group footers
ColumnDelimiter - determines the string that will separate the cells of the exported data. Default is comma ",". Available in GridViewCsvExportOptions only.
RowDelimiter - determines the string that will separate the rows of the exported data. Default is new line. Available in GridViewCsvExportOptions only.
UseSystemCultureSeparator - if set, the RadGridView will use the system List Separator string, specified in Control Panel's Regional Options, to separate cells. This property overrides the ColumnDelimiter property. Available in GridViewCsvExportOptions only.
Example 1 shows how to display a "Save File" dialog asking the user to save the file in excel format:
Example 1: Save RadGridView`s content in Excel file
void btnExport_Click(object sender, RoutedEventArgs e)
{
string extension = "xls";
SaveFileDialog dialog = new SaveFileDialog()
{
DefaultExt = extension,
Filter = String.Format("{1} files (*.{0})|*.{0}|All files (.)|.", extension, "Excel"),
FilterIndex = 1
};
if (dialog.ShowDialog() == true)
{
using (Stream stream = dialog.OpenFile())
{
gridViewExport.Export(stream,
new GridViewExportOptions()
{
Format = ExportFormat.Html,
ShowColumnHeaders = true,
ShowColumnFooters = true,
ShowGroupFooters = false,
});
}
}
}
Private Sub btnExport_Click(sender As Object, e As RoutedEventArgs)
Dim extension As String = "xls"
Dim dialog As New SaveFileDialog() With {
.DefaultExt = extension,
.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (.)|.", extension, "Excel"),
.FilterIndex = 1
}
If dialog.ShowDialog() = True Then
Using stream As Stream = dialog.OpenFile()
gridViewExport.Export(stream, New GridViewExportOptions() With {
.Format = ExportFormat.Html,
.ShowColumnHeaders = True,
.ShowColumnFooters = True,
.ShowGroupFooters = False
})
End Using
End If
End Sub
In addition, RadGridView provides built-in methods to get the content of your GridView control in different formats:
Text - each row is exported on new line (\r\n) with values separated by tabs (\t). In order to export to this format use the ToText() method.
CSV - each row is exported on new line (\r\n) with values surrounded by quotation marks and separated by commas. In order to export to this format use the ToCsv() method.
Html - the content of the RadGridView is exported in standard Html table. In order to export to this format use the ToHtml() method.
ExcelML - the content of the RadGridView is exported to Excel XML format. In order to export to this format use the ToExcelML() method.
The export methods (ToHtml(), ToCsv(), ToText() and ToExcelML()) are implemented in the class ExportExtension as extension methods to the standard RadGridView control. In order to use these methods you have to import the Telerik.Windows.Controls namespace.
Example 2: Save content to String
string htmlExport = this.gridViewExport.ToHtml(true);
Dim htmlExport As String = Me.gridViewExport.ToHtml(True)