Asp.net 操作Excel 无法关闭解决方法之一

来源:互联网 发布:淘宝营销代理 编辑:程序博客网 时间:2024/06/06 00:57

            if (range != null)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
                    range = null;
                }
                catch (Exception e1)
                {
                }
            }

            if (worksheet != null)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
                    worksheet = null;
                }
                catch (Exception e2)
                {
                }
            }
            if (workbook != null)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                    workbook = null;
                }
                catch (Exception e3)
                {
                }
            }
            if (workbooks != null)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
                    workbook = null;
                }
                catch (Exception e3)
                {
                }
            }
            try
            {

                KillExcelProcess(xlApp.Hwnd);
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
            if (xlApp != null)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                    xlApp = null;
                }
                catch (Exception e4)
                {
                }
            }
           
            GC.Collect();

 

 

 

这种方式经常关闭不了Excel,苦熬几个小时,测试无数,最后将站点中的bin目录中的excel.dll的security添加了everyone完全控制搞定了,我#¥#)(×)¥(#。

 

原创粉丝点击