C# 如何引用 Microsoft.Office.Interop.Excel dll?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15285880/
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
How to reference Microsoft.Office.Interop.Excel dll?
提问by Moe_Al
I had developed a system that deals with excel sheets in 2006 using MS VS 2005. Now, I can not use the same reference with MS VS 2012.
我在 2006 年使用 MS VS 2005 开发了一个处理 Excel 表格的系统。现在,我不能使用与 MS VS 2012 相同的参考。
var app = new Microsoft.Office.Interop.Excel.Application();
Workbooks wbs = app.Workbooks;
采纳答案by vivat pisces
Use NuGet (VS 2013+):
使用 NuGet (VS 2013+):
The easiest way in any recent version of Visual Studio is to just use the NuGet package manager. (Even VS2013, with the NuGet Package Manager for Visual Studio 2013extension.)
在任何最新版本的 Visual Studio 中,最简单的方法是使用 NuGet 包管理器。(甚至 VS2013,带有用于 Visual Studio 2013扩展的NuGet 包管理器。)
Right-click on "References"and choose "Manage NuGet Packages...", then just search for Excel.
右键单击“引用”并选择“管理 NuGet 包...”,然后搜索 Excel。
VS 2012:
与 2012 年相比:
Older versions of VS didn't have access to NuGet.
较旧版本的 VS 无法访问 NuGet。
- Right-click on "References" and select "Add Reference".
- Select "Extensions" on the left.
- Look for
Microsoft.Office.Interop.Excel
.
(Note that you can just type "excel" into the search box in the upper-right corner.)
- 右键单击“引用”并选择“添加引用”。
- 选择左侧的“扩展”。
- 寻找
Microsoft.Office.Interop.Excel
.
(请注意,您只需在右上角的搜索框中键入“excel”即可。)
VS 2008 / 2010:
与 2008 / 2010 年相比:
- Right-click on "References" and select "Add Reference".
- Select the ".NET" tab.
- Look for
Microsoft.Office.Interop.Excel
.
- 右键单击“引用”并选择“添加引用”。
- 选择“.NET”选项卡。
- 寻找
Microsoft.Office.Interop.Excel
.
回答by mas_oz2k1
You have to check which version of Excel you are targeting?
您必须检查您的目标是哪个版本的 Excel?
If you are targeting Excel 2010 use version 14 (as per Grant's screenshot answer), Excel 2007 use version 12 . You can not support Excel 2003 using vS2012 as they do not have the correct Interop dll installed.
如果您的目标是 Excel 2010 使用版本 14(根据 Grant 的屏幕截图答案),Excel 2007 使用版本 12 。您不能使用 vS2012 支持 Excel 2003,因为它们没有安装正确的 Interop dll。
回答by Muflix
If you have VS 2013 Express and you cant find Microsoft.Office namespace, try this ('Microsoft Excel 12.0 Object Library' if you want to use Office 2007)
如果你有 VS 2013 Express 并且你找不到 Microsoft.Office 命名空间,试试这个('Microsoft Excel 12.0 Object Library' 如果你想使用 Office 2007)
回答by LawMan
Instead of early binding the reference, there's an open source project called NetOffice that abstracts this from your project, making life much easier. That way you don't have to rely on your users having a specific version of Office installed.
有一个名为 NetOffice 的开源项目,它从您的项目中抽象出来,让生活变得更加轻松,而不是提前绑定参考。这样您就不必依赖安装了特定版本的 Office 的用户。
回答by Daniel.Veneziano
Building off of Mulfix's answer, if you have Visual Studio Community 2015, try Add Reference... -> COM -> Type Libraries -> 'Microsoft Excel 15.0 Object Library'.
基于 Mulfix 的答案,如果您有 Visual Studio Community 2015,请尝试添加引用... -> COM -> 类型库 -> 'Microsoft Excel 15.0 Object Library'。
回答by CYCLONE
You can also try installing it in Visual Studio via Package Manager.
您也可以尝试通过包管理器在 Visual Studio 中安装它。
Run Install-Package Microsoft.Office.Interop.Excel
in the Package Console.
This will automatically add it as a project reference.
Install-Package Microsoft.Office.Interop.Excel
在包控制台中运行。这将自动将其添加为项目参考。
Use is like this:
使用是这样的:
Using Excel=Microsoft.Office.Interop.Excel;