RadControls for Silverlight 导出excel

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:

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

 

copy
VB.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 (*.*)|*.*",_
  .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.

copy
C#
using Telerik.Windows.Controls;
...
string htmlExport = this.ExportGrid.ToHtml( true );

copy
VB.NET
Imports Telerik.Windows.Controls
...
Dim htmlExport As String = Me.ExportGrid.ToHtml( True )

引用:

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

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...