Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What is happening is that your call to:</p> <pre><code>Sheet.Cells[iRowCount, 1] = data["fullname"].ToString(); </code></pre> <p>Is essentially the same as:</p> <pre><code>Excel.Range cell = Sheet.Cells[iRowCount, 1]; cell.Value = data["fullname"].ToString(); </code></pre> <p>By doing it this way, you can see that you are creating an <code>Excel.Range</code> object, and then assigning a value to it. This way also gives us a named reference to our range variable, the <code>cell</code> variable, that allows us to release it directly if we wanted. So you could clean up your objects one of two ways:</p> <p>(1) The difficult and ugly way:</p> <pre><code>while (data.Read()) { Excel.Range cell = Sheet.Cells[iRowCount, 1]; cell.Value = data["fullname"].ToString(); Marshal.FinalReleaseComObject(cell); cell = Sheet.Cells[iRowCount, 2]; cell.Value = data["brand"].ToString(); Marshal.FinalReleaseComObject(cell); cell = Sheet.Cells[iRowCount, 3]; cell.Value = data["agency"].ToString(); Marshal.FinalReleaseComObject(cell); // etc... } </code></pre> <p>In the above, we are releasing each range object via a call to <code>Marshal.FinalReleaseComObject(cell)</code> as we go along.</p> <p>(2) The easy and clean way: </p> <p>Leave your code exactly as you currently have it, and then at the end you can clean up as follows:</p> <pre><code>GC.Collect(); GC.WaitForPendingFinalizers(); if (wSheet != null) { Marshal.FinalReleaseComObject(wSheet) } if (wBook != null) { wBook.Close(false, m_objOpt, m_objOpt); Marshal.FinalReleaseComObject(wBook); } xl.Quit(); Marshal.FinalReleaseComObject(xl); </code></pre> <p>In short, your existing code is <em>extremely close</em>. If you just add calls to GC.Collect() and GC.WaitForPendingFinalizers() before your 'NAR' calls, I think it should work for you. (In short, both Jamie's code and Ahmad's code are correct. Jamie's is cleaner, but Ahmad's code is an easier "quick fix" for you because you would only have to add the calls to calls to GC.Collect() and GC.WaitForPendingFinalizers() to your existing code.)</p> <p>Jamie and Amhad also listed links to the <a href="http://www.xtremevbtalk.com/forumdisplay.php?f=105" rel="nofollow noreferrer">.NET Automation Forum</a> that I participate on (thanks guys!) Here are a couple of related posts that I've made here on <a href="http://ww.StackOverflow.com" rel="nofollow noreferrer">StackOverflow </a>:</p> <p>(1) <a href="https://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c/159419#159419">How to properly clean up Excel interop objects in C#</a></p> <p>(2) <a href="https://stackoverflow.com/questions/981547/c-automate-powerpoint-excel/982014#982014">C# Automate PowerPoint Excel -- PowerPoint does not quit</a></p> <p>I hope this helps, Sean...</p> <p>Mike</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload