Opening Excel in Visual C#

Posted on 2009-05-13
Last Modified: 2013-12-17
I am seting up a C# program from scatch and have aded in the Microsoft Excel 11.0 Object Libary.

I can reference this with the code:
   using Microsoft.Office.Interop.Excel;

but I can't use:
  using Excel;

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

Is there soemthing obvious I'm missing?

Question by:Enzar

Accepted Solution

tculler earned 500 total points
ID: 24381265
When you specify the "using" directives at the top of your file, you're basically saying "If you happen to see an unqualified type, and its nested in the following namespace, just qualify the namespace for me because the names won't clash". The "." character is used either as just a character in the namespace's name, or to represent that a namespace is nested within another. For example, we could assume that the excel COM library's layout would look something like the following:

namespace Microsoft
     namespace Office
          namespace Interop
               namespace Excel
                    // Excel COM library implementation

When you compile your program, the compiler actually has no idea what "string" would be. That type doesn't exist (it's actually just an alias for the String class in the System namespace). Also, even though we may put

using System;

class MainMethod
     private static void Main()
          // Input prompt, yada yada
          String msg = Console.ReadLine();
          // Processing

the compiler has no idea what "String" is, or what "Console" is. Well, if "String" isn't the true name for the String class, then what is? Answer: System.String. A "fully-qualified" type-name consists of the namespace structure (such as Microsoft.Office.Interop.Excel.Workbook, vs just Workbook with a using Microsoft.Office.Interop.Excel at the top). Types must either be fully-qualified, or not qualified at all with a "using" statement at the top. In the case of the using statement, the compiler will actually qualify the name for you, so you don't have to! So, what's the point of namespaces then? Why not just use a "using" statement for everything? The answer is: because of type name clashing. For example, there are actually 3 different "Timers" in the .Net Framework (that I know of, at least), each within a different namespace: System.Threading, System.Windows.Forms, and System.Timers. Each has its own unique implementation, so if you create a new instance of one, and you have a using statement of two or more of the clashing Timers, how does the compiler know which object to utilize and instantiate? It doesn't, which is we we have namespaces.

Anyways, onto your problem. The general rule is: when specifying namespaces, namespaces must ALWAYS be fully-qualified. There's no namespace called "Excel" in any of the libraries you've referenced. Instead, the namespace you're *probably* looking for is Microsoft.Office.Interop.Excel, which you have a using statement for right near the top of your post. So, solution? Just remove the "using Excel;" part; the "Excel" namespace doesn't exist. It's "nested" within the other three ("Microsoft", "Office", and "Interop").

Hope that helps,

Author Comment

ID: 24382302
thanks for this.

I'm hacing trouble acces worksheet.range object. I can use the get_range but range isn't an option.

Any ideas, thanks.
            Microsoft.Office.Interop.Excel.Application excelApp = new ApplicationClass();
            excelApp.Visible = true;
            Workbook excelWorkbook = excelApp.Workbooks.Open(@"C:\Test.xls", 0, true, 2, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, null, null);
            Sheets excelSheets = excelWorkbook.Worksheets;
            Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(1);
            Microsoft.Office.Interop.Excel.OLEObject bButton = (OLEObject)excelWorksheet.OLEObjects("OptionButton1");
            string str1 = bButton.LinkedCell();
            // At this point excelWorksheet.range does not exist, .get_raneg does though.

Open in new window


Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question