c# and excel automation - ending the running instance

十年热恋 提交于 2019-12-25 01:25:20

问题


I got the same problem. I read the whole thread and tried the examples given. I added these lines to the sample code:

xlRange = (Excel.Range)xlWorkSheet.get_Range("B1", "C1");
xlRange.Merge(Type.Missing);
xlRange = (Excel.Range)xlWorkSheet.get_Range("H5", "M5"); 
xlRange.Merge(Type.Missing);
xlRange = (Excel.Range)xlWorkSheet.get_Range("N5", "V5");
xlRange.Merge(Type.Missing);
xlRange = (Excel.Range)xlWorkSheet.get_Range("W5", "Z5");
xlRange.Merge(Type.Missing);   // up to here everything seems fine

///////////////////////////////////////////////////////////
// if I add these lines below, the process just hanged until
// I manually close the form
xlRange = (Excel.Range)xlWorkSheet.get_Range("AA5", "AB5"); 
xlRange.Merge(Type.Missing); 
xlBorder.Borders.Weight = Excel.XlBorderWeight.xlThin;
///////////////////////////////////////////////////////////

I also added this, before releasing the excelsheet object: Marshal.ReleaseComObject(xlRange);

Please tell me how should I go about this as I am almost giving up on this. Your response is greatly appreciated. FYI, I'm a newbie and I am looking forward to your reply.

Thanks, george


回答1:


Excel is a COM Automation Server.

Even though you call Application.Quit() and you Release the Reference to the COM object, the exe itself will not end. You will still be able to see it in task manager. Making further calls to Excel will use the running instance.

The Excel instance will exit after your application closes.

Ever get "RPC server not found/running" type COM errors? Now you know why.




回答2:


I use this:

private void DoSomeStuff()
{
    var application = new Microsoft.Office.Interop.Excel.Application();
    // Do stuff ...
    CloseExcel(application);
}

private static void CloseExcel(Microsoft.Office.Interop.Excel.Application excel)
{
    while (Marshal.ReleaseComObject(excel) != 0) { }

    excel = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();
}

Works like a charm :)




回答3:


i've found this method to be most useful:

How to properly cleanup Excel interop objects in c#

keep in mind that you'll need to customize the code for your particular situation. i.e. if you create and hold a reference to the application, a workbook, and three range objects, you'll need to:

  1. Call GC.Collect()
  2. Call GC.WaitForPendingFinalizers()
  3. Call Marshal.FinalReleaseComObject for each of the range objects
  4. Close() the workbook
  5. Call Marshal.FinalReleaseComObject for the book object
  6. Close the application instance
  7. Call Marshal.FinalReleaseComObject for the application object

if you have seven range objects, or hold references to any other objects then you'll need to call Marshal.FinalReleaseComObject for each of those as well. the order in which you release everything is also very important.



来源:https://stackoverflow.com/questions/1826441/c-sharp-and-excel-automation-ending-the-running-instance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!