473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C# problem. Process Excel.exe not killed when application quit

Hi,

I'm new to C#. I'm writing a program that open a new excel file, write into it and lastly close the file. But the program is that there will be a orphaned process named EXCEL.EXE left on the process queue whenever the program quit. Anyway to terminate it automatically when the application exit? I read some previous postings on the same problem and try to use Marshal.ReleaseCOMObject and Garbage Collection. None of these works. Here's my coding:

Expand|Select|Wrap|Line Numbers
  1. SaveFileDialog s_Lvl_Max = newSaveFileDialog();
  2. s_Lvl_Max.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*";
  3. s_Lvl_Max.FilterIndex = 1;
  4. s_Lvl_Max.FileName = "filenamekk" + "_" + comboBox_baseTP.Text;
  5. s_Lvl_Max.OverwritePrompt = true;
  6. if (s_Lvl_Max.ShowDialog() == DialogResult.OK)
  7. {
  8. Excel.Application oXL;
  9. Excel._Workbook oWB;
  10. Excel._Worksheet oSheet;
  11. try
  12. {
  13. //Start Excel and get Application object.
  14. oXL = new Excel.Application();
  15. oXL.Visible = false;
  16. oXL.DisplayAlerts = false;
  17. //Get a new workbook.
  18. oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
  19. oSheet = (Excel._Worksheet)oWB.Worksheets[1];
  20. //Add table headers going cell by cell.
  21. oSheet.Cells[2, 1] = "Level ";
  22. oSheet.Cells[4, 1] = "Test";
  23. oSheet.Cells[4, 2] = "Frequency";
  24. oSheet.Cells[4, 3] = "Spec";
  25. oSheet.Cells[4, 4] = "Actual";
  26. oSheet.Cells[4, 5] = "Guard";
  27. //Format A11 as bold, vertical alignment = center.
  28. oSheet.get_Range("A1", "E1").Font.Bold = true;
  29. oSheet.get_Range("A1", "E1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
  30. // Create an array to multiple values at once.
  31. int i = 5;
  32. foreach (DataRow dr in dtable2.Rows)
  33. {
  34. oSheet.Cells[i, 1] = dr[0];
  35. oSheet.Cells[i, 2] = dr[1];
  36. oSheet.Cells[i, 3] = dr[2];
  37. oSheet.Cells[i, 4] = dr[3];
  38. oSheet.Cells[i, 5] = dr[4];
  39. i++;
  40. }
  41. i--;
  42. //AutoFit columns A.
  43. oSheet.get_Range("A1", "E1").EntireColumn.AutoFit();
  44.  
  45. oWB.SaveAs(s_Lvl_Max.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
  46. Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value,
  47. Missing.Value, Missing.Value, Missing.Value, Missing.Value);
  48. // Need all following code to clean up and extingush all references!!!
  49. oWB.Close(null, null, null);
  50. oXL.Workbooks.Close();
  51. oXL.Quit();
  52. //System.Runtime.InteropServices.Marshal.ReleaseComO bject(oRng);
  53. System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
  54. System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
  55. System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
  56. oSheet = null;
  57. oWB = null;
  58. oXL = null;
  59. GC.Collect(); // force final cleanup!
  60. MessageBox.Show(s_Lvl_Max.FileName + " has been successfully saved. ", "File Saved");
  61. }
  62. catch (Exception theException)
  63. {
  64. String errorMessage;
  65. errorMessage = "Error: ";
  66. errorMessage = String.Concat(errorMessage, theException.Message);
  67. errorMessage = String.Concat(errorMessage, " Line: ");
  68. errorMessage = String.Concat(errorMessage, theException.Source);
  69. Console.WriteLine(errorMessage, "Error");
  70. }
  71. }
  72.  
Any other way???
Oct 7 '08 #1
1 6934
l034n
15
You'll need to kill excel, as it's not quitting, even if you call Quit().

Expand|Select|Wrap|Line Numbers
  1. private void KillExcel()
  2. {
  3.     Process[] excelProcesses = System.Diagnostics.Process.GetProcessesByName("Excel");
  4.  
  5.     foreach(Process p in excelProcesses)
  6.     {
  7.         p.Kill();
  8.     }
  9. }
  10.  
Well, that will effectively kill all Excel processes, so if you have opened 10 excel instances outside your app, when you call this function all of them will close. If you want to prevent it, you should check for PID's.
Oct 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: David Conorozzo | last post by:
I create a new Access.Application instance. As soon as I refernce the property "References", I am unable to issue a truly successful quit on that instance. By that I mean the call to quit is fine...
8
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to...
5
by: Josema | last post by:
Hi to all, I have a windows application that uses workbooks, sheets, ranges, etc... This application is consumed by few clients. Some weeks ago I had problems to kill excel.exe process but i...
10
by: Lars-Erik Aabech | last post by:
Hi! This issue have been discussed a lot, but I haven't found a solution that applies to ASP.NET. I have a library which does some operations on Excel documents, and it will be used in an...
2
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft Office-XP PIA dll’s as these dll’s were been...
16
by: LP | last post by:
Hello, I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy...
6
by: Darrell Wesley | last post by:
A VB2003 application upgraded to VB2005 that builds an Excel spreadsheet. Everything appears to work correctly except that the Excel object does not go away it is still in the Process list in task...
0
by: henning.friese | last post by:
Hello NG, I'm need to write some code which creates tiff files from various document types (doc, pdf, xls). I want to do this by ShellExecuting (via System.Diagnostics.Process) the doc-files...
16
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.