C# 无法获得 Microsoft.Office.Interop 参考工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17822184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't get Microsoft.Office.Interop reference to work
提问by Ben Strombeck
I have a C# winforms app and I am simply trying to open an Excel sheet. When I try to add a reference to Microsoft.Office.Interop, the "Office" part is red and says "Can't resolve symbol 'Office'".
我有一个 C# winforms 应用程序,我只是想打开一个 Excel 工作表。当我尝试添加对 Microsoft.Office.Interop 的引用时,“Office”部分为红色并显示“无法解析符号‘Office’”。
When I attempt to build, the error is:
当我尝试构建时,错误是:
The type or namespace name 'Office' does not exist in the namespace 'Microsoft'
(are you missing an assembly reference?)
I have Office 2012 installed, and I think I have the Primary Interop Assemblies installed... but I'm not positive.
我安装了 Office 2012,我想我已经安装了主互操作程序集......但我并不乐观。
I know that this should be so easy, but I've been looking around for an answer to this for almost an hour and just can't figure it out. Thanks in advance!
我知道这应该很简单,但是我已经四处寻找答案将近一个小时了,只是想不通。提前致谢!
采纳答案by Matthew Jacobus
You need to add the library assembly reference to your project. They are referred to as "Primary Interop Assemblies".
您需要将库程序集引用添加到您的项目中。它们被称为“主要互操作程序集”。
(Assuming Visual Studio 2010)
(假设 Visual Studio 2010)
Procedure
程序
- open the solution explorer window
- expand your project folder accordion.
- right click on the references element
- select "add reference" from the dropdown
- select the .NET tab and look for the object library called
Microsoft.Office.Interop.Excel. - click ok
- 打开解决方案资源管理器窗口
- 展开您的项目文件夹手风琴。
- 右键单击引用元素
- 从下拉菜单中选择“添加引用”
- 选择 .NET 选项卡并查找名为
Microsoft.Office.Interop.Excel. - 单击确定
The object library should now appear in your references.
对象库现在应该出现在您的引用中。
回答by Abhay Kumar
I think you are missing the dll reference. Add Microsoft.Office.Interop.Excel.dll to project reference and then try.
我认为您缺少 dll 参考。将 Microsoft.Office.Interop.Excel.dll 添加到项目引用中,然后尝试。
回答by ybdesire
You need to install Office 2013 to clean this build error.
您需要安装 Office 2013 才能清除此构建错误。
回答by Mike Gledhill
I had the same error this morning, with a Winforms app which had always built perfectly in the past. All that had changed was that our company had upgraded our laptops from Excel 2007 to Excel 2013.
今天早上我遇到了同样的错误,使用过去一直完美构建的 Winforms 应用程序。唯一改变的是我们公司将我们的笔记本电脑从 Excel 2007 升级到 Excel 2013。
After some investigating, I realised that the app was a .Net 3.5 app, and although the Solution Explorer suggested that all the refererences were fine..
经过一番调查,我意识到该应用程序是 .Net 3.5 应用程序,尽管解决方案资源管理器建议所有引用都很好..
...actually, they weren't. The tell-tail sign was that, when I tried to re-add the References in the app, they couldn't be found...
……事实上,他们不是。告示牌是,当我尝试在应用程序中重新添加引用时,找不到它们...
What I needed to do was:
我需要做的是:
- update the app from .Net 3.5 to .Net 4.5
- remove the Office-related References (the first 5 shown in my first screenshot above)
- re-add the References (now shown as version 14 or 15)
- 将应用程序从 .Net 3.5 更新到 .Net 4.5
- 删除与 Office 相关的参考资料(上面我的第一个屏幕截图中显示的前 5 个)
- 重新添加引用(现在显示为版本 14 或 15)
I also had to change one line of code from:
我还必须更改一行代码:
excel = new Excel.ApplicationClass();
to
到
excel = new Excel.Application();
Once I'd done this, the app built without errors, and ran successfully again.
完成此操作后,该应用程序构建无错误,并再次成功运行。
回答by Behzad
回答by Simon Hutchison
Use NuGet Package Manager in VS2015
在 VS2015 中使用 NuGet 包管理器
- Right click references in your visual studio project
- Select Manage NuGet Packages
- Type
microsoft.officein the search box - Select Microsoft.Office.Interop.Excel
- Click Install
- Rebuild your solution
- 右键单击 Visual Studio 项目中的引用
- 选择管理 NuGet 包
- 键入
microsoft.office在搜索框中 - 选择 Microsoft.Office.Interop.Excel
- 点击安装
- 重建您的解决方案


