我在寫一個利用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