As of Q1 2010 (version number 2010.1.0309+), RadGridView has a new method - Export which gives you more control over which elements are included in the exported data.

It is the preferred method of exporting data. The method expects two parameters:

1. Stream - usually the file stream which you are exporting data to.

2. GridViewExportOptions or GridViewCsvExportOptions object - use it 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.

The following example shows how to show a save file dialog asking the user to save the file in excel format:

CopyC#
public MainPage()
{
 InitializeComponent();
 btnExport.Click += new RoutedEventHandler(btnExport_Click); 
}
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,
    });
  }
 }
}

 

CopyVB.NET
Public Sub New()
 InitializeComponent()
 AddHandler btnExport.Click, AddressOf btnExport_Click
End Sub
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 a built-in methods to get the content of your grid view 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.
Note

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 see and call these methods you have to import the Telerik.Windows.Controls namespace.

CopyC#
using Telerik.Windows.Controls;
...
string htmlExport = this.ExportGrid.ToHtml( true );
CopyVB.NET
Imports Telerik.Windows.Controls
...
Dim htmlExport As String = Me.ExportGrid.ToHtml( True )

引用:

http://www.telerik.com/help/silverlight/gridview-export.html