为什么无法从 Excel 互操作中设置强制转换对象?
尝试获取对工作表的引用(使用 Excel 互操作):
Excel.Application xl = new Excel.ApplicationClass();
Excel.Workbooks xlWorkBooks = xl.Workbooks;
Excel.Workbook xlWorkBook = xlWorkBooks.Open(fileName, 0, false, 5, "",
"", true, Excel.XlPlatform.xlWindows, "\t",
false, false, 0, true, 1, 0);
// Next line crashes
Excel.Worksheets xlWorkSheets = (Excel.Worksheets) xlWorkBook.Worksheets;
错误是它无法转换它:
无法将类型“System.__ComObject”的 COM 对象转换为接口类型“Microsoft.Office.Interop.Excel.Worksheets”。此操作失败,因为对 IID 为“{000208B1-0000-0000-C000-000000000046}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:不支持此类接口(HRESULT 异常:0x80004002 (E_NOINTERFACE)) .
我的演员阵容不正确吗?
Trying to get a reference to the worksheets (using Excel interop):
Excel.Application xl = new Excel.ApplicationClass();
Excel.Workbooks xlWorkBooks = xl.Workbooks;
Excel.Workbook xlWorkBook = xlWorkBooks.Open(fileName, 0, false, 5, "",
"", true, Excel.XlPlatform.xlWindows, "\t",
false, false, 0, true, 1, 0);
// Next line crashes
Excel.Worksheets xlWorkSheets = (Excel.Worksheets) xlWorkBook.Worksheets;
The error is that it cannot cast it:
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)).
Is my cast incorrect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,你的演员阵容有误。
_Workbook.Sheets
为您提供 工作表 实例。该界面为您提供所有类型的工作表,而不仅仅是工作表;主要包括图表、宏表等。另一方面,工作表界面只为您提供工作表,而不是图表。
接口之间不可互相分配;因此,您会收到 COM 错误。这很令人困惑 - 我什至不确定是否可以通过 PIA 获取
Worksheets
界面的实例 - 但这就是 Office Interop。只要您使用
_Workbook.Worksheets
属性而不是_Workbook.Sheets
属性,您就应该获得一个仅返回Sheets
的实例code>Worksheet 对象 - 尽管该界面能够提供其他类型的工作表。Yes, your cast is wrong.
_Workbook.Sheets
gives you a Sheets instance. This interface gives you all types of sheets, not just worksheets; mainly, it includes charts, macro sheets, etc.On the other hand, the Worksheets interface only gives you worksheets - not charts.
The interfaces are not assignable to each other; therefore, you get the COM error. It's confusing - I'm not even sure if it's possible to get an instance of the
Worksheets
interface through the PIA - but that's Office Interop for ya.As long as you use the
_Workbook.Worksheets
property instead of the_Workbook.Sheets
property, you should get an instance ofSheets
that only returnsWorksheet
objects - in spite of the fact that the interface is capable of providing other types of sheets.根据 MSDN、
Workbook.Worksheets
返回Microsoft.Office.Interop.Excel.Sheets
。因此,您可以像这样进行转换:
或者假设
Excel
映射到Microsoft.Office.Interop.Excel
(如您的问题所示)According to MSDN,
Workbook.Worksheets
returnsMicrosoft.Office.Interop.Excel.Sheets
.So you'd cast it like this:
Or assuming
Excel
maps toMicrosoft.Office.Interop.Excel
(as appears from your question)如果它适用于一种环境但不适用于另一种环境,请检查 HK Classes Root/TypeLib 注册表项。
您可能正在尝试跑步
HKCR\TypeLib{00020813-0000-0000-C000-000000000046}\1.6
但用户安装的东西已经添加了密钥:
HKCR\TypeLib{00020813-0000-0000-C000-000000000046}\1.7
导致 Interop 调用抛出异常。
或者,如果不是这样,则可能是 GAC 中的某个问题,因为操作系统版本不同。
我遇到了这个问题,它在运行 Windows 7 的开发人员计算机上运行,并在运行 XP 的用户计算机上导致此错误。
If it works on one environment but not another, check the HK Classes Root/TypeLib reg keys.
It is possible that you're trying to run for
HKCR\TypeLib{00020813-0000-0000-C000-000000000046}\1.6
but something the user installed has added the key:
HKCR\TypeLib{00020813-0000-0000-C000-000000000046}\1.7
causing the Interop call to throw an exception.
Or if that isn't it, it could be something in the GAC because of different OS versions.
I had this issue where it worked on our developer machines running Windows 7, and caused this error on a user's machine running XP.
奇怪的一个。根据 此页面,它的类型应该是
Sheets
而不是Worksheets
。还没有测试过 - 尝试一下?Odd one. According to this page, it's supposed to be of type
Sheets
notWorksheets
. Haven't tested - give it a whirl?如果您正在处理 Excel 2007+,我建议使用 System.IO.Packaging + System.Xml.Linq (LINQ to XML) 来操作 Excel 工作表。它更加干净,并且不需要在运行应用程序的计算机上实际安装 excel。
您还会遇到更少的 COM 冲突(例如您帖子中的上述内容)。
如果您尝试编辑 Excel 2003 或更早版本,那么很遗憾,我无法为您提供帮助。
If you're dealing with Excel 2007+ I'd suggest using System.IO.Packaging + System.Xml.Linq (LINQ to XML) to manipulate excel sheets. It's much cleaner and dosen't require excel to actually be installed on the machine you're running your app on.
You'll also run into less COM collisions (such as above in your post).
If you're trying to edit Excel 2003 or earlier, then unfortunately I'm unable to help you.
这可能是在升级到最新 Office 版本后发生的。尝试转到“程序和功能”,找到安装并“修复”它。显然,它更新和/或重新注册了用于创建这些对象的互操作 dll。
这可能需要结合 @codesforcoffee 描述的注册表修复来完成。
This might have happened after and upgrade to latest Office version. Try going to Programs and Features, finding the installation and 'repairing' it. Apparently, it updated and/or re-registers the interop dll used to create those objects.
This might need to be done in combination with the registry fix described by @codesforcoffee.