943,987 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 5658
  • C# RSS
Oct 6th, 2008
0

Cant end the excel process

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 37
Solved Threads: 7
Junior Poster
raul15791 is offline Offline
102 posts
since Jun 2008
Oct 6th, 2008
0

Re: Cant end the excel process

Try this... Not working either....

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 37
Solved Threads: 7
Junior Poster
raul15791 is offline Offline
102 posts
since Jun 2008
Nov 19th, 2008
0

Re: Cant end the excel process

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;
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zarget is offline Offline
1 posts
since Nov 2008
Nov 19th, 2008
0

Re: Cant end the excel process

Yeah I guess there's no complete solution for this problem. I found another solution which seems working as well:

C# Syntax (Toggle Plain Text)
  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!!
Reputation Points: 37
Solved Threads: 7
Junior Poster
raul15791 is offline Offline
102 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: asp.net 2005 sending email
Next Thread in C# Forum Timeline: VC# compile problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC