Microsoft.Office.Interop.Excel
-
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
-
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
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
-
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
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
-
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
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
-
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
-
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.
Have you included the Microsoft Interop Assemblies as references in your project?
-
Have you included the Microsoft Interop Assemblies as references in your project?
-
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?
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.