微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐