我在网上找到了一种杀死Excel 进程的方法,如下:
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
...
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
worksheets=null;worksheet=null; ...
excelApp=null;
range=null;
把操作Excel文件用到的对象实例全部释放。
然后资源回收!
GC.Collect();
说要写到finally中,我写了,可是编译时说worksheets 什么的都不存在,我想可能是需要先定义,可是我不知道具体的意思,请高手帮我解释一下具体操作的方法以及代码的意思。
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
...
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
worksheets=null;worksheet=null; ...
excelApp=null;
range=null;
把操作Excel文件用到的对象实例全部释放。
然后资源回收!
GC.Collect();
说要写到finally中,我写了,可是编译时说worksheets 什么的都不存在,我想可能是需要先定义,可是我不知道具体的意思,请高手帮我解释一下具体操作的方法以及代码的意思。
http://www.codeproject.com/csharp/package.asp
myApp=new Excel.ApplicationClass();
myApp.Visible = false;
oMissiong = System.Reflection.Missing.Value;
myBooks = myApp.Workbooks;
myBook = myBooks.Open(ExcelFile,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);
mySheet=(Excel.Worksheet)myBook.Sheets[1];
..........................
.........................
if(myBook != null)
myBook.Close(true, ExcelFile, oMissiong); // Quit Excel and clean up.
if(mySheet != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (mySheet);
mySheet = null;
if(myBook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (myBook);
myBook = null;
if(myBooks != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (myBooks);
myBooks = null;
if(myApp != null)
{
myApp.Workbooks.Close();
myApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (myApp);
myApp = null;
}
GC.Collect();
}
参照这个看一下
http://community.csdn.net/Expert/topic/4788/4788931.xml?temp=4.803103E-02