.net 使用COM对象将Silverlight Datagrid导出到Excel

.net 使用COM对象将Silverlight Datagrid导出到Excel,.net,vb.net,silverlight,excel,silverlight-4.0,.net,Vb.net,Silverlight,Excel,Silverlight 4.0,我在silverlight 4应用程序上有一个datagrid,我正试图通过调用silverlight中excel的COM组件将其导出到excel。它不断地出错,并告诉我该功能不受支持。我不太明白是什么错了,我相信我的代码是可靠的,但显然不是 Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Dim excel As Object = Automatio

我在silverlight 4应用程序上有一个datagrid,我正试图通过调用silverlight中excel的COM组件将其导出到excel。它不断地出错,并告诉我该功能不受支持。我不太明白是什么错了,我相信我的代码是可靠的,但显然不是

Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)

        Dim excel As Object = AutomationFactory.CreateObject("Excel.Application")

        excel.visible = True

        Dim workbook As Object = excel.workbooks

        workbook.add()

    Dim sheet As Object = excel.activesheet
    Dim cell As Object = Nothing
    Dim i As Integer = 1

    'populate the excel sheet
    For Each item In ActivityTypeCountDataGrid.ItemsSource

        cell = sheet.cells(i, 1)
        cell.Value = item.Activity_Type
        cell.ColumnWidth = 50

        cell = sheet.cells(i, 2)
        cell.Value = item.Activity_Type_Count
        cell.ColumnWidth = 50

        i += 1
    Next item



End Sub
这是数据网格

这里是错误 您有两个选择

1) 将此应用程序转换为Silverlight OOB应用程序


2) 将数据传递给WCF服务。让服务创建Excel工作簿。将文件(作为
字节[]
IEnumerable
等)传递回Silverlight应用程序,并提示用户使用
保存文件对话框保存文件

这是浏览器外应用程序吗?否,它在浏览器中运行您需要提高信任度才能工作。我相信您必须是OOB才能使用COM。datagrid正在从已加载到WCF服务的数据库中的视图中提取数据。因此,根据您的建议,我可以在WCF服务中编写一个方法,该方法可以根据已经提取数据的方法创建Excel工作簿,然后将其传递给应用程序前端的用户?是的,如果您的WCF服务已经可以访问数据,无需将其从Silverlight传递到服务。在WCF服务中写入此内容不太可能,因为我无法导入WCF服务中的Imports System.Runtime.InteropServices.Automation Excel需要安装在承载WCF服务的服务器上