【发布时间】:2011-03-09 03:06:08
【问题描述】:
我正在将 C# dotnet v1.1 应用程序升级到 3.5,并且第一次在 3.5 下运行时遇到此错误。这是一款在生产环境中运行良好的应用程序,并且在 1.1 中在我的桌面上运行,所以我假设 excel COM 的 API 在 1.1 和 3.5 之间发生了一些变化
完全例外:
InvalidClassCastException was unhandled
Unable to cast COM object of type 'System.__ComObject' to interface type 'Excel._Workbook'.
此操作失败,因为 IID 为“{000208DA-0000-0000-C000-000000000046}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败: 不支持此类接口(HRESULT 异常:0x80004002 (E_NOINTERFACE))。
代码:
Excel.ApplicationClass excel = new Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);
((Excel.Range)excel.Cells[1,1]).EntireRow.Font.Bold = true;
excel.Cells[1,1] = "col a";
excel.Cells[1,2] = "col b";
excel.Cells[1,3] = "col c";
// here loop and populate rows, nothing special just as above, all strings
excel.Visible = true;
Excel.Worksheet worksheet = (Excel.Worksheet)excel.ActiveSheet;
((Excel._Workbook)worksheet).Activate();
我该如何解决这个问题?我尝试在 MSDN Social 上将 Excel.ApplicationClass 重命名为 Excel.Application 为 this post,但没有运气:(
【问题讨论】:
-
一本书是一张纸的集合。将 WorkSheet 转换为 WorkBook 应该会发出一声巨响。对我来说,这在以前是如何起作用的很神秘。
-
一般...不在 PIA 中使用前缀为
_的类(和一些接口?)。这些应该被认为是“内部的”——PIA 层次结构很可能已经发生了变化。
标签: excel .net-3.5 com migration .net-1.1