Silverlight 利用Telerik导出到Word

来源:互联网 发布:数据备份与还原 编辑:程序博客网 时间:2024/03/29 23:00

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>");
                }
            }
        }
原创粉丝点击