一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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

时间:2010-07-30 编辑:简简单单 来源:一聚教程网

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.releasecomobject(xbk);
        system.runtime.interopservices.marshal.releasecomobject(excel);
        system.runtime.interopservices.marshal.releasecomobject(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//有一些核心进程是不允许访问的,因此而引起的异常可以捕获并放弃处理
                    { }
                }
                    
            }

热门栏目