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

asp.net生成xls文件完成后自动关闭excel进程代码

asp教程.net生成xls文件完成后自动关闭excel进程代码
//方法一

gc.collect();
        applicationclass excel;
        _workbook xbk;
        _worksheet xst;
        excel = new applicationclass();
        xbk = excel.workbooks.add(true);
        xst = (_worksheet)xbk.activesheet;
           //自定义代码...
        //结束时
        xbk.close(false, null, null);
        excel.quit();
        system.runtime.interops教程ervices.marshal.relea易做图object(xbk);
        system.runtime.interopservices.marshal.relea易做图object(excel);
        system.runtime.interopservices.marshal.relea易做图object(xst);
        xbk = null;
        excel = null;
        xst = null;

//方法二

 

finally
            {
                ((microexcel._application)tempapp).workbooks.close();
                int32 tempgeneration = gc.getgeneration(tempapp);
                ((microexcel._application)tempapp).quit();
                gc.collect(tempgeneration);
                //上面部分在winform中就已经干掉excel进程了,但是在asp.net教程中还不行          
                process[] myprocesses;
                myprocesses = process.getprocessesbyname("excel");

                //得不到excel进程id,暂时只能判断进程启动时间
                foreach (process myprocess in myprocesses)
                {
                    try
                    {
                        datetime starttime = myprocess.starttime;

                        if (starttime >= tempstart && starttime <= tempend)
                        {
                            myprocess.kill();
                        }
                    }//直接在vs中运行网页就不会有取不到starttime的问题
                    catch//有一些核心进程是不允许访问的,因此而引起的异常可以捕获并放弃处理
                    { }
                }
                    
            }

补充:asp.net教程,.Net开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,