C# project does not see Excel COM Reference

2019-05-29 11:00发布

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:

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

I also added a using clause:

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:

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

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.

Any ideas what can be wrong?

2条回答
霸刀☆藐视天下
2楼-- · 2019-05-29 11:35

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

查看更多
Explosion°爆炸
3楼-- · 2019-05-29 11:53

Try changing your using to:

using Excel = Microsoft.Office.Interop.Excel;

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

查看更多
登录 后发表回答