Common way to kill the Excel process
2007-04-12 16:32
357 查看
The code follows:
1string path = @"D:\\test.xls";
2 Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass();
3 Excel.Workbooks oWorkBooks = app.Workbooks;
4 Excel.Workbook oWorkBook = null;
5
6 oWorkBook = oWorkBooks.Open(path, Type.Missing, (object)false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
7 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
8
9 Excel.Sheets sheets = oWorkBook.Sheets;
10 Excel.Worksheet oWorkSheet = (Excel.Worksheet)sheets.get_Item(1);
11 oWorkSheet.get_Range("A1", Type.Missing);
12
13
14
15
16 //oWorkBook.Close(Type.Missing, Type.Missing, Type.Missing);
17
18 //oWorkBooks.Close();
19 NAR(oWorkSheet);
20 NAR(sheets);
21 //NAR(oWorkBook);
22 NAR(oWorkBooks);
23 app.Quit();
24 NAR(app);
25
26 GC.Collect();
1public void NAR(object o)
2 {
3 try
4 {
5 System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
6 }
7 catch
8 {
9 }
10 finally
11 {
12 o = null;
13 }
14 }
Notice:
Why we needn't release the Worksheet and Workbook COM object? That is we release the Worksheets and Workbooks object, who is the collection of Worksheet and Workbook, so the sub object will be release togother.
When we use the Worksheets and Workbooks object, and have not defiend them, the Excel process will not be killed. So before we use the Worksheets and Workbooks must to declare them! The detail code had been belowed.
Good Luck!
1string path = @"D:\\test.xls";
2 Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass();
3 Excel.Workbooks oWorkBooks = app.Workbooks;
4 Excel.Workbook oWorkBook = null;
5
6 oWorkBook = oWorkBooks.Open(path, Type.Missing, (object)false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
7 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
8
9 Excel.Sheets sheets = oWorkBook.Sheets;
10 Excel.Worksheet oWorkSheet = (Excel.Worksheet)sheets.get_Item(1);
11 oWorkSheet.get_Range("A1", Type.Missing);
12
13
14
15
16 //oWorkBook.Close(Type.Missing, Type.Missing, Type.Missing);
17
18 //oWorkBooks.Close();
19 NAR(oWorkSheet);
20 NAR(sheets);
21 //NAR(oWorkBook);
22 NAR(oWorkBooks);
23 app.Quit();
24 NAR(app);
25
26 GC.Collect();
1public void NAR(object o)
2 {
3 try
4 {
5 System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
6 }
7 catch
8 {
9 }
10 finally
11 {
12 o = null;
13 }
14 }
Notice:
Why we needn't release the Worksheet and Workbook COM object? That is we release the Worksheets and Workbooks object, who is the collection of Worksheet and Workbook, so the sub object will be release togother.
When we use the Worksheets and Workbooks object, and have not defiend them, the Excel process will not be killed. So before we use the Worksheets and Workbooks must to declare them! The detail code had been belowed.
Good Luck!
相关文章推荐
- How to get the process ID to kill a nohup process?
- The easy way to execute sudo command in Python using subprocess.Popen
- how to let the process kill itself?
- about to search or kill the process and the port in the linux
- [howto] General 5.10 - HowTo: Speed up ubuntu boot process - the way you can feel it. - updated
- vncserver Can't find file /root/.vnc/192.168.1.3:1.pid You'll have to kill the Xvnc process manually
- How to achieve the boot, clear cache, kill the process, floating windows single double-click to distinguish, with source
- how to check port number and kill process with the port number
- Attaching the script debugger to process *** failed on machine "***"
- Ubuntu 13.10 has no way to modify the keyboard layout
- ebs R12.2启动报错"failed to start a managed process after the maximum retry limit"
- 报错:failed to get the task for process XXX(解决方案)
- An easier way to register the Assemblies' Instrumentation In Enterprise Library[转]
- Simplest way to serve static data from outside the application server in a Java web application
- HOW TO Kill The Running Job in Oracle
- [已解决]Attaching the Scriptdebugger to process '[9376] iexplore.exe' on machine 'DELL' failed.
- Hide a process in the kill task menu
- What is the fastest, case insensitive, way to see if a string contains another string in C#?
- The way to fix Homebrew which can not update on mac.
- E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?