日期:2014-05-17  浏览次数:20861 次

excel导出进程无法关闭
我现在用C#写了一个winform应用程序,我现在是用线程导出Excel数据。数据可以成功导出,但是在任务管理器里的excel.exe进程就是无法随着数据导出结束而结束,怎么办呢?每导出一份数据就多一个excel.exe进程,郁闷

------解决方案--------------------

/// <summary>
        /// 预关闭未退出的Excel进程方法
        /// </summary>
        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;
                    }
                }
            }
        }

------解决方案--------------------
 我一直都这样做,很有用的:
 [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
private static void killExcel(Microsoft.Office.Interop.Excel.Application excel)
        {
            Process[] ps = Pro