[英]Unable to cast COM object '(Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets'
I am trying to read excel file through this way but getting error: 我正在尝试通过这种方式读取excel文件,但出现错误:
Microsoft.Office.Interop.Excel.Application appExcel;
Microsoft.Office.Interop.Excel.Workbook workbook;
Microsoft.Office.Interop.Excel.Range range;
Microsoft.Office.Interop.Excel._Worksheet worksheet;
appExcel = new Microsoft.Office.Interop.Excel.Application();
workbook = appExcel.Workbooks.Open(path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
// getting error on the below line
worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets;
Error is below.. 错误如下。
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel._Worksheet'.
无法将类型为“ System .__ ComObject”的COM对象转换为接口类型为“ Microsoft.Office.Interop.Excel._Worksheet”。 This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D8-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
此操作失败是因为由于以下错误,IID为'{000208D8-0000-0000-C000-000000000046}'的接口的COM组件上的QueryInterface调用由于以下错误而失败:不支持此类接口(HRESULT的异常:0x80004002(E_NOINTERFACE)) 。
have any idea about this exception? 对这个例外有任何想法吗?
Apparently, workbook.Sheets
is not a type of Worksheet
. 显然,
workbook.Sheets
不是Worksheet
。
worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets;
UPDATE: 更新:
Sheets is a collection of Worksheet
, so instead add an index: Sheets是
Worksheet
的集合,因此添加索引:
Microsoft.Office.Interop.Excel._Worksheet worksheet;
worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets[0];
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.