visual-studio C# 项目看不到 Excel COM 参考

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4386922/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:51:20  来源:igfitidea点击:

C# project does not see Excel COM Reference

c#visual-studioexcelreferenceautomation

提问by Robert Niestroj

im writing an application using Visual Studio 2010 Express Edition. I have a problem using Excel Automation. I added a COM Reference to Microsoft Excel 12.0 Object Library. It added following References:

我正在使用 Visual Studio 2010 Express Edition 编写应用程序。我在使用 Excel 自动化时遇到问题。我添加了对 Microsoft Excel 12.0 对象库的 COM 引用。它添加了以下参考资料:

Microsoft.Office.Core
Microsoft.Office.Interop.Excel 
VBIDE

I also added a using clause:

我还添加了一个 using 子句:

using Microsoft.Office.Interop.Excel;

Then i took a code snippet from microsoft:

然后我从微软拿了一个代码片段:

m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));

and it underlines Excel and shouts:

它在 Excel 下划线并喊道:

The type or namespace name 'Excel' could not be found (are you missing a using directive or an assembly reference?)

找不到类型或命名空间名称“Excel”(您是否缺少 using 指令或程序集引用?)

I have Office 2007 Professional Plus and the "Interoperability component for .net 2.0 Microsoft Forms"(sorry, translation from polish) installed. Target Framework is .NET 4 Client Profile.

我安装了 Office 2007 Professional Plus 和“.net 2.0 Microsoft Forms 的互操作性组件”(抱歉,波兰语翻译)。目标框架是 .NET 4 Client Profile。

Any ideas what can be wrong?

任何想法可能是错误的?

回答by Hans Olsson

Try changing your usingto:

尝试将您的更改using为:

using Excel = Microsoft.Office.Interop.Excel;

That way Excelwill be an alias for the full namespace, which the rest of the code seems to expect.

这种方式Excel将是完整命名空间的别名,其余代码似乎也期望如此。

回答by Harish

Can you try this... using Excel = Microsoft.Office.Interop.Excel; (as found from here)

你能试试这个吗...使用 Excel = Microsoft.Office.Interop.Excel; (从这里找到)