简体   繁体   English

无法转换COM对象'(Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets'

[英]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.

相关问题 如何修复无法将类型“object”隐式转换为“Microsoft.Office.Interop.Excel._Worksheet”。 存在显式转换 - how to fix Cannot implicitly convert type 'object' to 'Microsoft.Office.Interop.Excel._Worksheet'. An explicit conversion exists 无法将“microsoft.Office.Interop.Excel.ApplicationClass”类型的 COM 对象转换为“microsoft.Office.Interop.Excel.Application”” - unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'" 无法将“System._ComObject”类型的 COM object 转换为“microsoft.Office.Interop.Excel.Application”” - unable to cast COM object of type 'System._ComObject' to 'microsoft.Office.Interop.Excel.Application'" 无法转换“Microsoft.Office.Interop.Excel.WorksheetClass”类型的COM对象 - Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.WorksheetClass' 无法转换透明代理以键入“ Microsoft.Office.Interop.Excel.Worksheet” - Unable to cast transparent proxy to type 'Microsoft.Office.Interop.Excel.Worksheet' C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' - C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' C#-Microsoft.Office.Interop.Excel.Sheets到Microsoft.Office.Interop.Excel.Worksheet [] - C# - Microsoft.Office.Interop.Excel.Sheets to Microsoft.Office.Interop.Excel.Worksheet[] InvalidCastException-无法转换类型为“ Microsoft.Office.Interop.Outlook.ApplicationClass”的COM对象 - InvalidCastException - Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' 无法在Microsoft.Office.Interop.Word(Word 2016)中投射COM - Unable to cast COM in Microsoft.Office.Interop.Word (Word 2016) 空工作表-Microsoft.Office.Interop.Excel - Empty Worksheet - Microsoft.Office.Interop.Excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM