RadGridView导出到Excel会创建空文件

时间:2016-02-24 22:14:51

标签: wpf telerik radgridview

我在下面绑定了RadGridView,我正在尝试导出其内容,但它总是导致一个空的导出文件。

<telerik:RadGridView Grid.Row="0" x:Name="Logs"
                                 SelectedItem="{Binding SelectedLogEntry, Mode=TwoWay}">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn CellTemplateSelector="{StaticResource IconTemplateSelector}" IsResizable="False"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding LogLevel,Mode=OneWay}" Header="Level"
                                                CellStyle="{StaticResource PaddedCell}" MinWidth="50">
                    </telerik:GridViewDataColumn>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding LogID,Mode=OneWay}" Header="LogID"
                                                SortMemberPath="LogID" SortingState="Descending" MinWidth="50"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding MachineDateTime,Mode=OneWay}"
                                                Header="Machine Time" CellStyle="{StaticResource PaddedCell}" MinWidth="150"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Occured, Mode=OneWay}" Header="Occured" MinWidth="100" CellStyleSelector="{StaticResource ColorSelector}"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding System,Mode=OneWay}" Header="System" MinWidth="50"
                                                CellStyle="{StaticResource PaddedCell}" />
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Mode,Mode=OneWay}" Header="Mode" MinWidth="50"
                                                CellStyle="{StaticResource PaddedCell}" />
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding CallingClass,Mode=OneWay}"
                                                Header="Calling Class" MinWidth="250" Width="Auto"
                                                CellStyle="{StaticResource PaddedCell}" />
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Message,Mode=OneWay}" Header="Message"
                                                Width="250" CellStyle="{StaticResource PaddedCell}" />
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Exception,Mode=OneWay}" Header="Exception" Width="350" CellStyle="{StaticResource PaddedCell}" />  
                </telerik:RadGridView.Columns>                      
</telerik:RadGridView>

导出日志的方法:

  private void ExportLogs()
        {
            using (var stream = File.Create(filepath))
            {
                Logs.Export(stream,
                 new GridViewExportOptions()
                 {
                     Format = ExportFormat.ExcelML,
                     ShowColumnHeaders = true,
                     ShowColumnFooters = false,
                     ShowGroupFooters = false,
                 });    

            }
        }

未收到任何例外或警告。 任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

我的代码的问题在于,由于多个线程,GridView是从非UI线程访问的,这导致GridView的内容处于无法访问状态。所以我尝试从UI线程调用Export函数,它按预期工作。