在C#中创建Excel工作簿时,类未注册的错误 [英] Class not registered error when creating Excel workbook in C#

查看:333
本文介绍了在C#中创建Excel工作簿时,类未注册的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用以下代码访问Excel电子表格时,在安装了Office 2007(版本12)的Visual Studio 2012中定义工作簿对象wrkbuk时,会收到库未注册错误

  Microsoft.Office.Interop.Excel.Application excapp = new Microsoft.Office.Interop.Excel.Application(); 
string bookname = @ C:\Users\Public\Documents\RECRUITMENT& SELECTION\MOVEMENTS\MOVEMENTS\Monscellaneous Documents\VacanciesREAL.xls;
工作簿wrkbuk = excapp.Workbooks.Open(bookname);
工作表wrksht = new Worksheet();

错误详细信息是



System.InvalidCastException未处理
HResult = -2147467262
消息=无法将类型为Microsoft.Office.Interop.Excel.ApplicationClass的COM对象转换为接口类型Microsoft.Office.Interop.Excel._Application,此操作失败,因为由于以下错误,IID'{000208D5-0000-0000-C000-000000000046}接口的COM组件上的QueryInterface调用失败:库未注册(HRESULT异常:0x8002801D(TYPE_E_LIBNOTREGISTERED))
Source = mscorlib



我在VS 2012附带的Office 14主要Interop程序集中创建了对Microsoft.Office.Interop.Excel.dll的引用, Office 12版本的dll,既不解决问题。我已经尝试使用regasm注册dll,但这也没有帮助。



我可以在以下行创建工作表wrksht OK与Office 14和Office 12 dll所以问题似乎只影响工作簿定义。



2013年12月17日



尝试重新安装Office 2007无效发现这个解决方案,这有效。这是在



http://social.msdn.microsoft.com/Forums/vstudio/en-US/d3f92da7-96d3- 404b-89d0-d236800ceae5 / vs-2012-rc-and-visual-studio-tools-for-office?forum = vsto



问题是Office的多个版本 - 我有Office 14的代码,可能来自VS 2012安装


我怀疑VS 2012 RC已经安装了Office 2013年输入
库,现在已经注册了重复的版本。我有一个
这个特定的错误消息('无法演变
_Application'....'TYPE_E_LIBNOTREGISTERED'),然后重新安装旧版本的Office。我解决了
按照另一个论坛线程在这里的建议,我很伤心
无法找到...基本上我搜索注册表中的
中的CLSID错误消息,所以, {00020970-0000-0000-C000-000000000046}。它的
注册表项包含两个键,其中一个被称为TypeLib,
,这又包含了类型库的另一个CLSID。然后
搜索注册表,从头开始,再次为
第二个CLSID,这导致我进入相关的interop类型库。它
有两个条目.... 8.3和8.4,更大的数字对应于
更新版本的Office ....我删除...,并立即可以
运行我的程序。



解决方案

我有这个完全相同的问题。以下是我如何修复:



转到HKEY_CLASSES_ROOT\TypeLib\并搜索Office.Interop.Excel。



我发现HKEY_CLASSES_ROOT\TypeLib\(guid)\1.8,但其子节点为空! 我删除它,因为没有安装Office 15我们的VSTO项目安装程序或我们自己的自定义安装程序必须放在1.8文件夹中。



我在这里找到解决方案:



访问COM组件时出错


When I try to access an Excel spreadsheet using the following code I get a "Library not registered' error when defining the workbook object wrkbuk using C# from Visual Studio 2012 with Office 2007 (ver 12) installed

Microsoft.Office.Interop.Excel.Application excapp = new Microsoft.Office.Interop.Excel.Application();
        string bookname = @"C:\Users\Public\Documents\RECRUITMENT & SELECTION\MOVEMENTS\MOVEMENTS\Miscellaneous Documents\VacanciesREAL.xls";
        Workbook wrkbuk = excapp.Workbooks.Open(bookname); 
        Worksheet wrksht = new Worksheet();

The error details are

System.InvalidCastException was unhandled HResult=-2147467262 Message=Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)). Source=mscorlib

I've created a reference to Microsoft.Office.Interop.Excel.dll in the Office 14 Primary Interop Assembly that comes with VS 2012 and also for the Office 12 version of the dll and neither fixes the problem. I've tried registering the dlls with regasm but this does not help either.

I can create the worksheet wrksht on the following line OK with the Office 14 and Office 12 dlls so the problem seems only affect workbook definition.

Dec 17 2013

Tried reinstalling Office 2007 to no avail but found this solution, which worked. It was at

http://social.msdn.microsoft.com/Forums/vstudio/en-US/d3f92da7-96d3-404b-89d0-d236800ceae5/vs-2012-rc-and-visual-studio-tools-for-office?forum=vsto

Problem is code from multiple versions of Office - I had code from Office 14, possibly coming from VS 2012 install

I suspect that VS 2012 RC has installed the Office 2013 type libraries, and that there are now duplicate versions registered. I got that specific error message ('unable to cast _Application'....'TYPE_E_LIBNOTREGISTERED') after a messy uninstall, and then re-installing an older version of Office. I solved it by following the advice of another forum thread here which I'm sadly unable to find...basically I searched the registry for the CLSID in the error message, so, {00020970-0000-0000-C000-000000000046}. Its registry key contained two keys, one of which was called 'TypeLib', which in turn contained another CLSID for the type library. I then searched the registry, starting at the beginning again, for that second CLSID, which led me to the relevant interop type library. It had two entries....8.3 and 8.4, the bigger number corresponding to the later version of Office....which I deleted...and immediately was able to run my program.

解决方案

I had this exact same problem. Here's how I fixed it:

Go to HKEY_CLASSES_ROOT\TypeLib\ and search for "Office.Interop.Excel".

I found HKEY_CLASSES_ROOT\TypeLib\(guid)\1.8 but its child nodes were empty! I deleted it because Office 15 wasn't installed on that machine, and hey presto - it worked.

Either our VSTO project installer, or our own custom installer must have placed the 1.8 folder there.

I found the solution here:

Error accessing COM components

这篇关于在C#中创建Excel工作簿时,类未注册的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆