Solved

Opening Excel in Visual C#

Posted on 2009-05-13
2
846 Views
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?

Thanks
0
Comment
Question by:Enzar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
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
          Console.WriteLine(msg);
     }
}

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,
Nate
0
 

Author Comment

by:Enzar
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

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 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