Creating a new excel workbook with C#
-
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
-
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
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
-
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
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
-
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
no, just you have to add references.. 1) rightclick your solution file. 2)click add reference. 3)add the reference file.
Padmanabhan
-
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
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
-
no, just you have to add references.. 1) rightclick your solution file. 2)click add reference. 3)add the reference file.
Padmanabhan
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