469,610 Members | 2,060 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

C# closing Excel the right way

this is the correct way to close excel with C#. I've seen alot of other bogus posts ALL over the web that don't work, how frustrating.

Expand|Select|Wrap|Line Numbers
  1. string savepath;
  2. bool foundPID; 
  3. int ourPID = 0;
  4. int tmpX = 0;
  5. int indexRow = 1;
  6. int indexCol = 1;
  7. int[] existingPIDs;
  8. existingPIDs = new int[100];
  9.  
  10. // get existing excel processes
  11. Process[] existingExcels = Process.GetProcessesByName("excel");
  12.  
  13.  
  14. savepath = System.Windows.Forms.Application.ExecutablePath.ToString();
  15. savepath = savepath.Substring(0, savepath.LastIndexOf("\\"));
  16. savepath = savepath + "\\Excel-Really-Sucks-Badly.xls";
  17.  
  18. //Excel Application Object
  19. Excel.Application oExcelApp;
  20. Excel.Workbook oExcelWorkbook;
  21. Excel.Worksheet oExcelWorksheet;
  22.  
  23. // build array of already existing excel process IDs
  24. foreach (Process proc in existingExcels)
  25. {
  26.     existingPIDs[tmpX++] = proc.Id;
  27. }
  28.  
  29. // create our new excel app
  30. oExcelApp = new Excel.Application();
  31.  
  32. // build up the list again, this time it should include our new excel app
  33. existingExcels = Process.GetProcessesByName("excel");
  34.  
  35. // find out which of the excel process IDs is our new app
  36. foreach (Process proc in existingExcels)
  37. {
  38.     foundPID = false;
  39.     foreach (int p in existingPIDs)
  40.     {
  41.         if (p == proc.Id)
  42.         {
  43.             foundPID = true;
  44.         }
  45.     }
  46.     if(!foundPID){
  47.         ourPID = proc.Id;
  48.     }
  49. }
  50.  
  51. oExcelApp.DisplayAlerts = false;
  52. oExcelApp.Visible = false;
  53. oExcelWorkbook = oExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
  54. oExcelWorksheet = (Excel.Worksheet)oExcelWorkbook.Sheets["Sheet1"];
  55.  
  56. oExcelWorksheet.Name = "fakehitswizard.com";
  57.  
  58. // write excel worksheet header
  59. oExcelWorksheet.Cells[indexRow, indexCol] = "I hate Excel";
  60.  
  61. indexRow++;
  62.  
  63. // save excel
  64. oExcelWorkbook.SaveAs(savepath, Excel.XlFileFormat.xlWorkbookNormal, "", "", true, false, Excel.XlSaveAsAccessMode.xlNoChange, false, false, false, false, false);
  65. // close the workbook
  66. oExcelWorkbook.Close(true, savepath, System.Reflection.Missing.Value );
  67. // quit excel 
  68. oExcelApp.Quit();
  69.  
  70. // Unfortunately the above alone should work but it doesn't because microsoft is weak sauce
  71.  
  72.  
  73. Process[] localByName = Process.GetProcessesByName("excel");
  74. // user didnt have any excels open, kill excel
  75. if (tmpX == 0)
  76. {
  77.     foreach (Process proc in localByName)
  78.     {
  79.         proc.Kill();
  80.     }
  81. }
  82. // user does have excel(s) already open, only kill our apps excel
  83. else if (tmpX > 0 && ourPID != 0)
  84. {
  85.     foreach (Process proc in localByName)
  86.     {
  87.         if (proc.Id == ourPID)
  88.         {
  89.             proc.Kill();
  90.         }
  91.     }
  92. }
  93.  
notice none of this Marshal junk, or GC.collect bogus.

try it out, open task manager, start up 0,1,2,3,4,5 or 99 excels, then run your C# app, notice task manager only starts and closes your app's excel, none of the others. Fake Hits Wizard
Oct 11 '07 #1
1 2428
JosAH
11,448 Expert 8TB
Hi, you posted your code solution in the .NET Articles section. Your post isn't
an article (yet): the code needs a bit of explanatory text just to help people to
understand what it's all about.

We can either move your post to the .NET Forum section where everybody can
read it as it is now or we can move your post over to the Editor's Corner where
we can turn your post into a real article. It's up to you. If you don't respond I'll
move your post over to the .NET Forum section.

kind regards,

Jos
Oct 12 '07 #2

Post your reply

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

Similar topics

4 posts views Thread by federico | last post: by
2 posts views Thread by Atley | last post: by
7 posts views Thread by rdemyan via AccessMonster.com | last post: by
2 posts views Thread by Silgd1 | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.