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

Silverlight 利用Telerik导出到Word

1,添加导出和打印按钮

                <StackPanel Orientation="Horizontal" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="20,0">
                    <Button x:Name="ExportButton" Content="导出" Click="ExportButton_Click"/>
                    <Button x:Name="PrintButton" Content="打印" Click="ExportButton_Click"/>
                </StackPanel>

2,定义

        private RadGridView grid = new RadGridView();
        private GridViewExportOptions options = new GridViewExportOptions();

3,初始化

            grid.ElementExported += new EventHandler<GridViewElementExportedEventArgs>(MainDataGridView_ElementExported);
            grid.AutoGenerateColumns = false;
            options.ShowColumnHeaders = true;
            options.Format = ExportFormat.Html;

            options.Encoding = System.Text.Encoding.UTF8;

4,利用Telerik的RadGridView进行导出

        public T_Bas_EnterList Model         {             get { return this.DataContext as T_Bas_EnterList; }         }

        private void ExportButton_Click(object sender,RoutedEventArgs e)         {             SaveFileDialog dialog = new SaveFileDialog();             dialog.DefaultExt = "doc";             dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*","doc","Word");             dialog.FilterIndex = 1;             if (dialog.ShowDialog() == true)             {                 using (Stream stream = dialog.OpenFile())                 {                     if (Model != null)                     {                         grid.ItemsSource = new List<T_Bas_EnterList>() { Model };                         grid.Columns.Add(new GridViewDataColumn()                         {                             Header = "企业详细信息",DataMemberBinding = new System.Windows.Data.Binding("entername")                         });                     }                     grid.Export(stream,options);                 }             }         }         void DataGridView_ElementExported(object sender,GridViewElementExportedEventArgs e)         {             if (e.Element == ExportElement.Row)             {                 if (Model != null)                 {                     e.Writer.Write(String.Format(@"<tr><td style=""background-color:#CCC;"" colspan=""{0}"">",                        ((IEnumerable<Telerik.Windows.Controls.GridViewColumn>)((RadGridView)sender).Columns).Count()));                     e.Writer.Write("<h2>基本信息</h2>");                     e.Writer.Write(String.Format(@"<b>污染源:</b> {0} <br />",Model.code_pollute));                     e.Writer.Write(String.Format(@"<b>企业名称:</b> {0} <br />",Model.entername));                     e.Writer.Write(String.Format(@"<b>企业类型:</b> {0} <br />",Model.code_enterrelation));                     e.Writer.Write(String.Format(@"<b>企业类型:</b> {0} <br />",Model.code_qualification));                     e.Writer.Write(String.Format(@"<b>企业类型:</b> {0} <br />",Model.code_enterrelation));                     e.Writer.Write("<h2>监测数据</h2>");                     MemoryStream memoryStream = new MemoryStream();                     DataGridView.Export(memoryStream,options);                     memoryStream.Flush();                     e.Writer.Write(System.Text.Encoding.UTF8.GetChars(memoryStream.ToArray()));                     e.Writer.Write("</td></tr>");                 }             }         }

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

相关推荐