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. Creating a new excel workbook with C#

Creating a new excel workbook with C#

Scheduled Pinned Locked Moved C#
csharpvisual-studio
6 Posts 2 Posters 0 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.
  • J Offline
    J Offline
    Jeffrey Webster
    wrote on last edited by
    #1

    Hi, I need to work with Excel via C#, but I'm having problems getting started. Keeping it simple to start, I'd just like to open a new Excel workbook. MSDN describes using code like this: objExcel = new Excel.Application(); objBooks = (Excel.Workbooks)m_objExcel.Workbooks; objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt)); Yet Excel is not recognized by the compiler, which I'm assuming is because I haven't exposed the right namespace. I'm not sure what that would be. Intellisense is not offering Excel as an option anywhere I've looked. Elsewhere I've heard about using ADO.NET, but haven't seen anything I can get to work. If anyone has worked with Excel via C# I'd appreciate any guidance... Thanks, Jeff

    P 1 Reply Last reply
    0
    • J Jeffrey Webster

      Hi, I need to work with Excel via C#, but I'm having problems getting started. Keeping it simple to start, I'd just like to open a new Excel workbook. MSDN describes using code like this: objExcel = new Excel.Application(); objBooks = (Excel.Workbooks)m_objExcel.Workbooks; objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt)); Yet Excel is not recognized by the compiler, which I'm assuming is because I haven't exposed the right namespace. I'm not sure what that would be. Intellisense is not offering Excel as an option anywhere I've looked. Elsewhere I've heard about using ADO.NET, but haven't seen anything I can get to work. If anyone has worked with Excel via C# I'd appreciate any guidance... Thanks, Jeff

      P Offline
      P Offline
      padmanabhan N
      wrote on last edited by
      #2

      you have to use the name space : using Microsoft.Office.Interop.Excel; And this helps to open the excel: Microsoft.Office.Interop.Excel.Application m_objExcel; Microsoft.Office.Interop.Excel.Workbooks m_objBooks; Microsoft.Office.Interop.Excel._Workbook m_objBook; Microsoft.Office.Interop.Excel.Sheets m_objSheets; Microsoft.Office.Interop.Excel._Worksheet m_objSheet; Microsoft.Office.Interop.Excel._Worksheet m_objSheetIntro; Microsoft.Office.Interop.Excel.Range m_objRange; object m_objOpt = System.Reflection.Missing.Value; m_objExcel = new Microsoft.Office.Interop.Excel.Application(); m_objBooks = (Microsoft.Office.Interop.Excel.Workbooks)m_objExcel.Workbooks; m_objBooks.Open("Your excel file path here", m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);

      Padmanabhan

      J 1 Reply Last reply
      0
      • P padmanabhan N

        you have to use the name space : using Microsoft.Office.Interop.Excel; And this helps to open the excel: Microsoft.Office.Interop.Excel.Application m_objExcel; Microsoft.Office.Interop.Excel.Workbooks m_objBooks; Microsoft.Office.Interop.Excel._Workbook m_objBook; Microsoft.Office.Interop.Excel.Sheets m_objSheets; Microsoft.Office.Interop.Excel._Worksheet m_objSheet; Microsoft.Office.Interop.Excel._Worksheet m_objSheetIntro; Microsoft.Office.Interop.Excel.Range m_objRange; object m_objOpt = System.Reflection.Missing.Value; m_objExcel = new Microsoft.Office.Interop.Excel.Application(); m_objBooks = (Microsoft.Office.Interop.Excel.Workbooks)m_objExcel.Workbooks; m_objBooks.Open("Your excel file path here", m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);

        Padmanabhan

        J Offline
        J Offline
        Jeffrey Webster
        wrote on last edited by
        #3

        Hi, Thanks for your reply. For some reason my compiler is not recognizing the Office part of the namespace. If I use Intellisense after Microsoft it gives me [Contracts, Csharp, Internal, SqlServer, VisualBasic and Win32.] Not Office. I'm using C# Express. Could that be the problem. Maybe I need a higher level version? Jeff

        P J 2 Replies Last reply
        0
        • J Jeffrey Webster

          Hi, Thanks for your reply. For some reason my compiler is not recognizing the Office part of the namespace. If I use Intellisense after Microsoft it gives me [Contracts, Csharp, Internal, SqlServer, VisualBasic and Win32.] Not Office. I'm using C# Express. Could that be the problem. Maybe I need a higher level version? Jeff

          P Offline
          P Offline
          padmanabhan N
          wrote on last edited by
          #4

          no, just you have to add references.. 1) rightclick your solution file. 2)click add reference. 3)add the reference file.

          Padmanabhan

          J 1 Reply Last reply
          0
          • J Jeffrey Webster

            Hi, Thanks for your reply. For some reason my compiler is not recognizing the Office part of the namespace. If I use Intellisense after Microsoft it gives me [Contracts, Csharp, Internal, SqlServer, VisualBasic and Win32.] Not Office. I'm using C# Express. Could that be the problem. Maybe I need a higher level version? Jeff

            J Offline
            J Offline
            Jeffrey Webster
            wrote on last edited by
            #5

            Hi, Okay, I added two references: Office 12.0 and Excel 11.0. So now the compiler recognizes *Office* but claims never to have seen *Interop* The article called *Excel Interop Use in C#* by Sam Allen amazingly doesn't mention where you would find these references. I'm seeing the solution you've described crop up on the net but I'm really not seeing how to wake up the compiler and get it to smell the namespaces. Jeff

            1 Reply Last reply
            0
            • P padmanabhan N

              no, just you have to add references.. 1) rightclick your solution file. 2)click add reference. 3)add the reference file.

              Padmanabhan

              J Offline
              J Offline
              Jeffrey Webster
              wrote on last edited by
              #6

              Hi, Again, thanks for your replies. It's a minor miracle, but at least the system is recognizing the namespace. I'm not sure what has changed since numerous times I tried different combinations of Excel Object refs and Office Object Library refs (earlier/later versions).Nothing seemed to work. I tried one last time by deleting as many refs as possible and referencing only Excel 11.0 Objects. Suddenly it worked. I'm saving this as-is and will experiment later to see if I can replicate the bug. Thanks, Jeff

              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