请问C#调用excel操作后在close方法保存文件后怎么清除和结束excel的进程???
我保存完excel文件后,发现excel不会自动释放和关闭,每次调用他进行操作后
都会有新的一个实例,结果程序运行一段时间后它excel的实例有很多个了,
占用资源,请问这个问题怎么解决。?

解决方案 »

  1.   

    '杀死Excel进程
            Dim myproc As System.Diagnostics.Process = New System.Diagnostics.Process
            Dim proc As Process
            Dim procs() As Process = Process.GetProcessesByName("excel")   '得到所有打开的进程
            Try
                For Each proc In procs
                    If Not proc.CloseMainWindow() Then
                        proc.Kill()
                    End If
                Next
            Catch
            End Tryhttp://blog.csdn.net/chengking/archive/2005/11/29/539514.aspx
      

  2.   

    上面的兄弟是个办法,但我想尽量在程序中用正当途径关闭操作后能够令excel自动关闭。
    有什么好的办法啊,我发现如果是应用程序每当完成操作后可以用cg回收机制来
    使得excel关闭,但web程序不知道怎么做
      

  3.   

    你是在本机上还是在网络上?
    如果是网络上的话,需要把服务器端的Excel的安全级别设置得低一点,默认 客户端是不允许对服务端的Excel进行关系操作的。
    如果是本地,那么应该是可以的,你调用Close函数把当前的WorkBooks关闭,然后调用Quit把当前的Excel退出。除非就是你当前的操作Excel的那个窗口没有退出,你想你点击一个按钮,在这个里面写一个Excel.Application m_excel = new Excel.Application(); 那么你看他在内存中是否增加一个Excel的对象呢?所以窗口不退出的话,在你的进程中还是会看到Excel.Exe,这个就是由于你New 了一个对象,GC还没有回收。如果你包含操作Excel的这个窗体关闭掉的话,那么你的Excel.exe应该也会没掉。大致代码如下:
      Excel.Application m_excel = new Excel.Application();
      m_excel.Workbooks.Add(true);
      m_excel.Visible = true;
      //其中的一些操作  
      m_excel.Workbooks.Close();
      m_excel.Quit();
      m_excel = null;
    你可以测试下
      

  4.   

    我测试过了,
      Excel.Application m_excel = new Excel.Application();
      m_excel.Workbooks.Add(true);
      m_excel.Visible = true;
      //其中的一些操作  
      m_excel.Workbooks.Close();
      m_excel.Quit();
      m_excel = null;以上对web后台的操作无效,excel进程还是一个一个的增加
      

  5.   

    参见:
    http://www.cnblogs.com/linfuguo/archive/2006/03/23/357155.html
      

  6.   

    参考:
    namespace ConsoleExcel2
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime t = DateTime.Now;
                Excel.Application app = new Excel.Application();
                if (app == null)
                {
                    return;
                }            Workbooks wbs = app.Workbooks;
                _Workbook wb = wbs.Add(@"D:\WinProjects\xxx.xls");
                Sheets shs = wb.Sheets;
                _Worksheet sh = (_Worksheet)shs.get_Item(1);
                if (sh == null)
                {
                    return;
                }            try
                {
                    string cellName;
                    Range r;
                    object o;
                    int i;
                    for (i = 2; i < 65535; i++)
                    {
                        cellName = "C" + i.ToString();
                        o = sh.Cells[i, "C"];
                        if (o == null)
                        {
                            break;
                        }
                    }
                    Console.WriteLine(i.ToString());
                }
                catch
                {
                }
                finally
                {
                    ReleaseCOM(sh);
                    ReleaseCOM(shs);
                    ReleaseCOM(wb);
                    ReleaseCOM(wbs);
                    app.Quit();
                    ReleaseCOM(app);
                }
                Console.WriteLine(t.ToString() + "  " + DateTime.Now.ToString());
                Console.Read();
            }        private static void ReleaseCOM(object o)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
                }
                catch
                {
                }
                finally
                {
                    o = null;
                }
            }
        }
    }