简体   繁体   English

如何引用 Microsoft.Office.Interop.Excel dll?

[英]How to reference Microsoft.Office.Interop.Excel dll?

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;

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. 在任何最新版本的Visual Studio中最简单的方法是使用NuGet包管理器。 (Even VS2013, with the NuGet Package Manager for Visual Studio 2013 extension.) (甚至是VS2013,带有用于Visual Studio 2013扩展的NuGet包管理器 。)

Right-click on "References" and choose "Manage NuGet Packages..." , then just search for Excel. 右键单击“引用”并选择“管理NuGet包...” ,然后只搜索Excel。

在此输入图像描述


VS 2012: VS 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 . 查找Microsoft.Office.Interop.Excel
    (Note that you can just type "excel" into the search box in the upper-right corner.) (请注意,您只需在右上角的搜索框中输入“excel”即可。)

VS2012/2013 References


VS 2008 / 2010: VS 2008/2010:

  • Right-click on "References" and select "Add Reference". 右键单击“引用”,然后选择“添加引用”。
  • Select the ".NET" tab. 选择“.NET”选项卡。
  • Look for Microsoft.Office.Interop.Excel . 查找Microsoft.Office.Interop.Excel

VS 2010 References

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. 在Package Console中运行Install-Package Microsoft.Office.Interop.Excel This will automatically add it as a project reference. 这将自动将其添加为项目引用。

Use is like this: 用法是这样的:

Using Excel=Microsoft.Office.Interop.Excel;

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命名空间,请尝试此操作(如果要使用Office 2007,请使用“Microsoft Excel 12.0对象库”)

在此输入图像描述

根据Mulfix的答案,如果你有Visual Studio Community 2015,请尝试添加参考... - > COM - >类型库 - >'Microsoft Excel 15.0对象库'。

Instead of early binding the reference, there's an open source project called NetOffice that abstracts this from your project, making life much easier. 而不是早期绑定引用,有一个名为NetOffice的开源项目,它从您的项目中抽象出来,使生活变得更加容易。 That way you don't have to rely on your users having a specific version of Office installed. 这样,您就不必依赖安装了特定Office版本的用户。

NetOffice Codeplex site. NetOffice Codeplex站点。

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 . 如果您的目标是Excel 2010使用版本14(根据Grant的屏幕截图答案),Excel 2007将使用版本12。 You can not support Excel 2003 using vS2012 as they do not have the correct Interop dll installed. 您无法使用vS2012支持Excel 2003,因为它们没有安装正确的Interop dll。

Go to Visual Studio Installer under Individual Components and click 'Office Developer Tools for Visual Studio. Go 到单个组件下的 Visual Studio 安装程序,然后单击“Visual Studio 的 Office 开发人员工具”。 This solved it for me after lots of headaches.经过很多头痛后,这为我解决了这个问题。

转到单个组件下的 Visual Studio 安装程序,然后单击“Visual Studio 的 Office 开发人员工具”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM