我在写一个利用Microsoft.Office.Interop.Excel程序集的类。它是一个“一站式”DLL库的一部分,将用于java解决方案(限制java端的接口数量)。无法将类型为'System .__ ComObject'的COM对象转换为接口类型'Microsoft.Office.Interop.Excel.Worksheets'
我收到以下错误:
Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Worksheets'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208B1-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
这是由下面的代码抛出:
Public Class XL
Public XL As Excel.Application = Nothing
Public XLN As String
Public WBS As Excel.Workbooks = Nothing
Public WBSN() As String
Public WB As Excel._Workbook = Nothing
Public WBN As String
Public WSS As Excel.Worksheets = Nothing
Public WSSN() As String
Public WS As Excel._Worksheet = Nothing
Public WSN As String
Public XLCelllValue As Object = Nothing
Public Sub New()
XL = New Excel.Application()
XL.Visible = True
WBS = XL.Workbooks
WB = WBS.Add()
WSS = WB.Worksheets '<this is the line that throws the exception
WS = WSS(1)
End Sub
End Class
我不知道我做错了所有的属性定义为public ,Worksheets是一个有效的集合,WB属性类型为Excel._workbook,WSS属性类型为Excel.worksheets。
任何想法我失踪?
作为我遇到的答案的补充[为什么不能从Excel互操作中设置对象?](http://stackoverflow.com/questions/2695229/why-cant-set-cast-an-对象来自excel-interop)和[Excel互操作:_Worksheet或Worksheet?](http://stackoverflow.com/questions/1051464/excel-interop-worksheet-or-worksheet)这可能对你有用。 – Bugs