当前位置:编程学习 > C#/ASP.NET >>

导出excel 时释放资源的问题!


            System.Runtime.InteropServices.Marshal.ReleaseComObject(wSheet);
            
            wBook.Close(true, strFilePath, System.Reflection.Missing.Value);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wBooks);
            xlApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
我做了以上步骤,但发现进程里面excel有N个 excel.exe! 
       

--------------------编程问答-------------------- 理论上是还有对excel的引用 实际上是 我也搞不出来 同问 --------------------编程问答-------------------- 需要自己杀掉excel进程 --------------------编程问答--------------------
遍历进程
然后
Process.kill --------------------编程问答--------------------
 public void PreExitExcel()
        {
            System.Diagnostics.Process[] allProcess = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process thisprocess in allProcess)
            {
                string processName = thisprocess.ProcessName;
                if (processName.ToLower() == "excel")
                {
                    try
                    {
                        thisprocess.Kill();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                        return;
                    }
                }
            }
        }
--------------------编程问答-------------------- LZ以上代码是 关闭名为excel的进程 --------------------编程问答-------------------- 需要添加引用com中的"microsoft excel 11.0 object library"

//关闭excel进程
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
                if (xlapp != null)
                {
                    xlapp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp);
                    foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
                    {
                        //先关闭图形窗口。如果关闭失败...有的时候在状态里看不到图形窗口的excel了,  
                        //但是在进程里仍然有EXCEL.EXE的进程存在,那么就需要杀掉它:p  
                        if (theProc.CloseMainWindow() == false)
                        {
                            theProc.Kill();
                        }
                    }
                    xlapp = null;
                }
--------------------编程问答--------------------     [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]

        public static extern int SetProcessWorkingSetSize(IntPtr process,

            int minSize,

            int maxSize

);



        /// <summary>  

        /// 释放内存  

        /// </summary>  

        public static void ClearMemory()
        {

            GC.Collect();

            GC.WaitForPendingFinalizers();

            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {

                Man.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);

            }

        }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,