Cant end the excel process

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 89
Reputation: raul15791 is an unknown quantity at this point 
Solved Threads: 7
raul15791 raul15791 is offline Offline
Junior Poster in Training

Cant end the excel process

 
0
  #1
Oct 6th, 2008
Hi, I have problem to terminate the process EXCEL.EXE after I write into it. After the program finish execute. The EXCEL.EXE is still in the proceses list in the task manager. Anyway to terminate it?

Below is my coding:

  1. SaveFileDialog s_Lvl_Max = new SaveFileDialog();
  2. s_Lvl_Max.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*";
  3. s_Lvl_Max.FilterIndex = 1;
  4. s_Lvl_Max.FileName = "FileKK" + "_" + comboBox_baseTP.Text;
  5. s_Lvl_Max.OverwritePrompt = true;
  6.  
  7. if (s_Lvl_Max.ShowDialog() == DialogResult.OK)
  8. {
  9. Excel.Application oXL;
  10. Excel._Workbook oWB;
  11. Excel._Worksheet oSheet;
  12. Excel.Range oRng;
  13.  
  14. try
  15. {
  16. //Start Excel and get Application object.
  17. oXL = new Excel.Application();
  18. oXL.Visible = false;
  19. oXL.DisplayAlerts = false;
  20.  
  21. //Get a new workbook.
  22. oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
  23. oSheet = (Excel._Worksheet)oWB.Worksheets[1];
  24.  
  25. //Add table headers going cell by cell.
  26. oSheet.Cells[2, 1] = "Level (Max VCC) Guardband";
  27. oSheet.Cells[4, 1] = "Test Name";
  28. oSheet.Cells[4, 2] = "Frequency";
  29. oSheet.Cells[4, 3] = "[Spec]";
  30. oSheet.Cells[4, 4] = "Actual";
  31. oSheet.Cells[4, 5] = "temp(V)";
  32.  
  33. //Format A1:D1 as bold, vertical alignment = center.
  34. oSheet.get_Range("A1", "E1").Font.Bold = true;
  35. oSheet.get_Range("A1", "E1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
  36.  
  37. // Create an array to multiple values at once.
  38. int i = 5;
  39.  
  40. foreach (DataRow dr in dtable2.Rows)
  41. {
  42. oSheet.Cells[i, 1] = dr[0];
  43. oSheet.Cells[i, 2] = dr[1];
  44. oSheet.Cells[i, 3] = dr[2];
  45. oSheet.Cells[i, 4] = dr[3];
  46. oSheet.Cells[i, 5] = dr[4];
  47.  
  48. i++;
  49. }
  50.  
  51. i--;
  52.  
  53. //AutoFit columns A:D.
  54. oRng = oSheet.get_Range("A1", "E1");
  55. oRng.EntireColumn.AutoFit();
  56.  
  57. oWB.SaveAs(s_Lvl_Max.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
  58. Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value,
  59. Missing.Value, Missing.Value, Missing.Value, Missing.Value);
  60. oXL.Quit(); // <--------- try to use this to terminate the process, not working
  61. MessageBox.Show("File has been successfully saved. :)");
  62. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 89
Reputation: raul15791 is an unknown quantity at this point 
Solved Threads: 7
raul15791 raul15791 is offline Offline
Junior Poster in Training

Re: Cant end the excel process

 
0
  #2
Oct 6th, 2008
Try this... Not working either....

  1. // Need all following code to clean up and extingush all references!!!
  2. oWB.Close(null,null,null);
  3. oXL.Workbooks.Close();
  4. oXL.Quit();
  5. System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng);
  6. System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
  7. System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
  8. System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
  9. oSheet=null;
  10. oWB=null;
  11. oXL = null;
  12. GC.Collect(); // force final cleanup!
Last edited by raul15791; Oct 6th, 2008 at 4:11 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 1
Reputation: zarget is an unknown quantity at this point 
Solved Threads: 0
zarget zarget is offline Offline
Newbie Poster

Re: Cant end the excel process

 
0
  #3
Nov 19th, 2008
It may be over a month since you posted but for what it's worth I ran across the same issue and could not find any help on the web so I came up with this code that does the trick. This code loops through all the Excel processes and when it finds the one with the same window title as the application you started it ends that process then exits the loop.

using System.Diagnostics;

foreach (Process p in Process.GetProcessesByName("EXCEL"))
{
if (oXL.Caption == p.MainWindowTitle)
{
p.Kill();
break;
}
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 89
Reputation: raul15791 is an unknown quantity at this point 
Solved Threads: 7
raul15791 raul15791 is offline Offline
Junior Poster in Training

Re: Cant end the excel process

 
0
  #4
Nov 19th, 2008
Yeah I guess there's no complete solution for this problem. I found another solution which seems working as well:

  1. // Need all following code to clean up and extingush all references!!!
  2. oWB.Close(null, null, null);
  3. oXL.Workbooks.Close();
  4. Process[] pProcess;
  5. pProcess = System.Diagnostics.Process.GetProcessesByName("Excel");
  6. pProcess[0].Kill();
  7.  
  8. oSheet = null;
  9. oWB = null;
  10. oXL = null;

Thanks anyway guys for all the help given!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC