如何关闭excel进程?
我在网上也查了些代码,但是有点问题。程序如下:
 //新建个实力,系统中生成一个excel进程
 editExcel = new Excel.Application(); //如果在这个地方操作的话,可以结束进程。
 System.Runtime.InteropServices.Marshal.ReleaseComObject(editExcel);
 editExcel = null;
 GC.Collect();失败情况如下:
 //新建个实力,系统中生成一个excel进程
 editExcel = new Excel.Application();
 //下面这段不太理解,也是在网上查的,不些之后的代码会出错
 Excel.Workbooks workBooks = editExcel.Application.Workbooks;
 workBooks.Open(fileName, missing, missing, missing,
   missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //再执行上面可用的代码则无效,进程还在
 System.Runtime.InteropServices.Marshal.ReleaseComObject(editExcel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBooks);
 editExcel = null;
 workBooks = null;
 GC.Collect();
是否和open有关呢?还是open后开了新的文件??请高手帮忙~~~~~~~
 

解决方案 »

  1.   

    excel进程关闭的问题都是个老大难的问题了。
    我原来在一个项目中用过。也是偶尔会关闭不了。。
      

  2.   

    我这样写的都能正常退出,你试试吧。
    引用Interop.Excel.dll                        Excel.Application appExcel = null;
    try
    {
    appExcel = new Excel.Application();
                                    //你的操作
                                         appExcel.Quit();
                                         return;
    }
    catch 
    {
    } if (appExcel != null) appExcel.Quit();
      

  3.   

    把WorkBook Close一下、Application Quit一下试试
      

  4.   

    感谢EricJaking,lake_cx只实例化Excel.Application然后退出确实可行,WorkBook单纯Close之后却无法关闭进程。怀疑在操作excel时候调用workbook的open方法有关
      

  5.   

    楼主 请提供一份 C# winform 调用COM组件的教程 感激不尽 我想了解 C#里如何使用 excel的COM组件