I would like to know the best way to work with excel in a dotnet windows app. I am currently using the getobject(). I am trying to retrive data from a worksheet and importing it into a dataset. I am using excel version 9.0. I understand that version 10 has xml componets already in it but am not able to upgrade at this point.
Thanks
Al

best way to use excel with dotnet windows apps
Sinem
<a href="http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnoxpta/html/odc_oxppias.asp">http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnoxpta/html/odc_oxppias.asp</a>
Chenxia
Dim xl As New Excel.Application
' Use the XL variable
System.Runtime.InteropServices.Marshal.ReleaseComObject(xl)
You can also use the Imports statement to clean this up:
Imports System.Runtime.InteropServices.Marshal
' later:
ReleaseComObject(xl)
The problem is that you may need to be very careful about "intermediate" objects you create. For example, if you use code like this:
xl.ActiveWorkbook.Cells(1, 1).SomeMethod
you may find that even releasing the Application object doesn't release Excel from memory. The problem is that when you refer to any object in Excel, they create an internal reference to that object under the covers. Releasing Excel doesn't release that reference, so Excel won't "die". I've found that in versions without PIAs, you may need to EXPLICITLY create a reference to each and every object you want to use, and release that object reference explicitly. That is, you might need to write code like this, instead:
Dim xl As ExcelApplication = New Excel.Application
xl.OpenWorkbook("YourWorkbook.xls")
Dim wb As Excel.WorkBook = xl.ActiveWorkbook
Dim xlCell As Excel.Range = wb.Cells(1, 1)
' and then
ReleaseComObject(xlCell)
ReleaseComObject(wb)
ReleaseComObject(xl)
That's just "air code" -- that is, I made it up in the editor, and it probably won't compile and run, but you get the idea. One tip I've learned: decide what you want Excel to do, and get it working first in VB6 or some other VBA host. Get it working, and verify that once the code completes, you've released Excel from memory. If you can't get it working in VB6, it will never work in .NET. And it's easier to program COM objects from VB6, I've found. Once you get the code working there, copy it over into VS.NET, and then start working on it as you see here, to make sure you release all the objects you're using. It's not particularly fun. Way easier with later versions of Excel.
Touraj
Chartsiam
thargy
Ah, your message says you can't upgrade. In that case, you'll definitely want to investigate downloading the Primary Interop Assemblies from Microsoft's site, so that you don't have to generate the interop assembly for each project you create, and you get the best behavior of the unmanaged objects (Excel has some weird problems removing itself from memory, for example, if you don't use the PIA.)
Jason Dolinger
tp_hi
Miah Helpmann
Thanks
Al
Matt W.