best way to use excel with dotnet windows apps

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


Answer this question

best way to use excel with dotnet windows apps

  • Sinem

    I havent't used them yet, but Microsoft offers something called "Primary Interop Assemblies" for Office. You might want to take a look at these:
    <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

    Really nothing to it:

    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

    Do you have any examples of using ReleaseComObject method 
  • Chartsiam

    The Primary Interop Assemblies are for Office XP (i.e. version 10).  Our friend Al said he's using version 9, which is Office 2000.  It's not looking to me like there will be a painless way to do this with Office 2K.


  • thargy

    If you can upgrade to Excel 2003, the best solution (and it's very much better) is to use Visual Studio Tools for Office, which allows you to react to events raised by Excel in managed code, without doing any work to set it up. If your users want to be able to simply load an Excel workbook, and run your application, this is a great solution. You can find info about the product <a href="http://msdn.microsoft.com/vstudio/office">here</a>, and an article introducing the product from MSDN magazine <a href="http://msdn.microsoft.com/msdnmag/issues/03/09/MicrosoftOffice2003/default.aspx">here</a>.

    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

    Make sure your friend finds the ReleaseComObject method in the framework--without that, memory problems are almost guaranteed!
  • tp_hi

    Ah. I can never remember which version number (9, 10, 11 ) corresponds to which real version name. Sorry. No PIAs. Gonna be self-generated interop assemblies, and a lot of careful memory handling ( and it's a real, solid PITA to make sure Excel gets released from memory). 
  • Miah Helpmann

    I thank you for your advise. I did get excel to work for me. And I am still having problems with the release of exel out of memory. I've been a big fan of yours for years. It's really kool for my VBA hero actually answered one of my questions.

    Thanks
    Al

  • Matt W.

    Also, no matter which version of Excel you're using, I recommend <a href="http://msdn.microsoft.com/library/default.asp url=/library/en-us/odc_vsto2003_ta/html/ExcelObj.asp">this (very long) article</a>, as it has information for any version. The downloadable example is written for Excel 2003, but the same exact concepts apply to earlier versions.
  • best way to use excel with dotnet windows apps