Solved

Opening Excel in Visual C#

Posted on 2009-05-13
2
832 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
2 Comments
 
LVL 9

Accepted Solution

by:
tculler earned 500 total points
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now