Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Microsoft.Office.Interop.Excel

Microsoft.Office.Interop.Excel

Scheduled Pinned Locked Moved C#
csharpvisual-studioasp-netcomtools
8 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spinz2112
    wrote on last edited by
    #1

    Im trying to write a utility that opens an excel file using C#.net Visual Studio 2005. One of the first things I have to do is add a reference to the Microsoft Excel 11 Object Library. After I add this I have to add the "using" statement to use Microsoft.Office.Interop.Excel. However, I cant seem to be able to find the Interop class. Intellisense only picks up Microsoft.Office.Core ...has anyone else experienced this? How did you add this class? Thanks

    T 1 Reply Last reply
    0
    • S spinz2112

      Im trying to write a utility that opens an excel file using C#.net Visual Studio 2005. One of the first things I have to do is add a reference to the Microsoft Excel 11 Object Library. After I add this I have to add the "using" statement to use Microsoft.Office.Interop.Excel. However, I cant seem to be able to find the Interop class. Intellisense only picks up Microsoft.Office.Core ...has anyone else experienced this? How did you add this class? Thanks

      T Offline
      T Offline
      Torsten Mauz
      wrote on last edited by
      #2

      Your using statement will be

      using Microsoft.Office.Core;

      Just because I've been through this pain before and the documentation for automatation is awful, I thought I'd paste an old demo I had lying around...

      private void ReadExcelValue()
      {
      object o = System.Reflection.Missing.Value;

      Excel.Application objE = new Excel.Application();
      Excel.Workbooks objBooks = null;
      Excel.Workbook objB = null;
      Excel.Worksheet objS = null;
      
      objE.Workbooks.Open(Server.MapPath("spreadsheet.xls"), o, o, o, o, o, o, o, o, o, o, o, o, o, o);
      objBooks = objE.Workbooks;
      objB = objBooks\[(object)1\];
      
      objS = (Excel.Worksheet)objB.Worksheets\[(object)1\];
      
      //write out the text in cell "A1"
      Response.Write(((Excel.Range)objS.Cells\[1, 1\]).Text);
      
      Release(objS);
      
      if (objB != null)
          objB.Close(false, o, o);
      Release(objB);
      
      if (objBooks != null)
          objBooks.Close();
      Release(objBooks);
      
      if (objE != null)
          objE.Quit();
      Release(objE);
      
      GC.Collect();
      
      System.Diagnostics.Process\[\] objPr = System.Diagnostics.Process.GetProcessesByName("Excel");
      foreach (System.Diagnostics.Process pr in objPr)
          pr.Kill();
      

      }

      private void Release(object obj)
      {
      try
      {
      System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
      }
      catch { }
      finally
      {
      obj = null;
      }
      }

      Note: It's rather ugly stuff and there's a cheesy hack to make sure that the excel process doesn't persist on the server after execution (yes it's pretty easy to end up with thousands of Excel.exe processes if you don't kill them). HTH

      S 1 Reply Last reply
      0
      • T Torsten Mauz

        Your using statement will be

        using Microsoft.Office.Core;

        Just because I've been through this pain before and the documentation for automatation is awful, I thought I'd paste an old demo I had lying around...

        private void ReadExcelValue()
        {
        object o = System.Reflection.Missing.Value;

        Excel.Application objE = new Excel.Application();
        Excel.Workbooks objBooks = null;
        Excel.Workbook objB = null;
        Excel.Worksheet objS = null;
        
        objE.Workbooks.Open(Server.MapPath("spreadsheet.xls"), o, o, o, o, o, o, o, o, o, o, o, o, o, o);
        objBooks = objE.Workbooks;
        objB = objBooks\[(object)1\];
        
        objS = (Excel.Worksheet)objB.Worksheets\[(object)1\];
        
        //write out the text in cell "A1"
        Response.Write(((Excel.Range)objS.Cells\[1, 1\]).Text);
        
        Release(objS);
        
        if (objB != null)
            objB.Close(false, o, o);
        Release(objB);
        
        if (objBooks != null)
            objBooks.Close();
        Release(objBooks);
        
        if (objE != null)
            objE.Quit();
        Release(objE);
        
        GC.Collect();
        
        System.Diagnostics.Process\[\] objPr = System.Diagnostics.Process.GetProcessesByName("Excel");
        foreach (System.Diagnostics.Process pr in objPr)
            pr.Kill();
        

        }

        private void Release(object obj)
        {
        try
        {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        }
        catch { }
        finally
        {
        obj = null;
        }
        }

        Note: It's rather ugly stuff and there's a cheesy hack to make sure that the excel process doesn't persist on the server after execution (yes it's pretty easy to end up with thousands of Excel.exe processes if you don't kill them). HTH

        S Offline
        S Offline
        spinz2112
        wrote on last edited by
        #3

        am i still missing any namespaces apart from the Microsoft.Office.Core library? cause I dont have the Application or Workbook classes in the Excel library (maybe im not stating that correctly). When I type Excel. intellisense does not pick up Application or Workbook...do I need any thing else? Thanks

        T 1 Reply Last reply
        0
        • S spinz2112

          am i still missing any namespaces apart from the Microsoft.Office.Core library? cause I dont have the Application or Workbook classes in the Excel library (maybe im not stating that correctly). When I type Excel. intellisense does not pick up Application or Workbook...do I need any thing else? Thanks

          T Offline
          T Offline
          Torsten Mauz
          wrote on last edited by
          #4

          I only had two references I added to get that code working were: Microsoft Office 11.0 Object Library Microsoft Excel 11.0 Object Library Obviously those will vary dependant on which version of office you are developing against. Intellisense partially works in web developer express, and vs 2005. I do seem to remember that it pretty much didn't work at all in older versions of visual studio (but not 100% sure on that). It's all part of the pain of doing office automation I'm afraid X| HTH

          S 1 Reply Last reply
          0
          • T Torsten Mauz

            I only had two references I added to get that code working were: Microsoft Office 11.0 Object Library Microsoft Excel 11.0 Object Library Obviously those will vary dependant on which version of office you are developing against. Intellisense partially works in web developer express, and vs 2005. I do seem to remember that it pretty much didn't work at all in older versions of visual studio (but not 100% sure on that). It's all part of the pain of doing office automation I'm afraid X| HTH

            S Offline
            S Offline
            spinz2112
            wrote on last edited by
            #5

            i cant even compile the project let alone get intellisense to work. i think im missing some namespace...the pain with VS 2005. im waiving the white flag..help.

            D 1 Reply Last reply
            0
            • S spinz2112

              i cant even compile the project let alone get intellisense to work. i think im missing some namespace...the pain with VS 2005. im waiving the white flag..help.

              D Offline
              D Offline
              Drew McGhie
              wrote on last edited by
              #6

              Have you included the Microsoft Interop Assemblies as references in your project?

              S 1 Reply Last reply
              0
              • D Drew McGhie

                Have you included the Microsoft Interop Assemblies as references in your project?

                S Offline
                S Offline
                spinz2112
                wrote on last edited by
                #7

                I did do an add reference, and the on the COM tab chose Microsoft Excel 10 Object Library. Does this suffice? Do you have to manually install any other interop libraries?

                D 1 Reply Last reply
                0
                • S spinz2112

                  I did do an add reference, and the on the COM tab chose Microsoft Excel 10 Object Library. Does this suffice? Do you have to manually install any other interop libraries?

                  D Offline
                  D Offline
                  Drew McGhie
                  wrote on last edited by
                  #8

                  I believe that should be sufficient. You might, however, need the Excel 11 libraries, which are included on the install disk for excel if you have 2003 or 2003 Pro (But NOT 2003 basic). Once you have the right one included, you should be able to access the correct namespaces/methods.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups