| | |
Cant end the excel process
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2008
Posts: 89
Reputation:
Solved Threads: 7
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:
Below is my coding:
C# Syntax (Toggle Plain Text)
SaveFileDialog s_Lvl_Max = new SaveFileDialog(); s_Lvl_Max.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*"; s_Lvl_Max.FilterIndex = 1; s_Lvl_Max.FileName = "FileKK" + "_" + comboBox_baseTP.Text; s_Lvl_Max.OverwritePrompt = true; if (s_Lvl_Max.ShowDialog() == DialogResult.OK) { Excel.Application oXL; Excel._Workbook oWB; Excel._Worksheet oSheet; Excel.Range oRng; try { //Start Excel and get Application object. oXL = new Excel.Application(); oXL.Visible = false; oXL.DisplayAlerts = false; //Get a new workbook. oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value)); oSheet = (Excel._Worksheet)oWB.Worksheets[1]; //Add table headers going cell by cell. oSheet.Cells[2, 1] = "Level (Max VCC) Guardband"; oSheet.Cells[4, 1] = "Test Name"; oSheet.Cells[4, 2] = "Frequency"; oSheet.Cells[4, 3] = "[Spec]"; oSheet.Cells[4, 4] = "Actual"; oSheet.Cells[4, 5] = "temp(V)"; //Format A1:D1 as bold, vertical alignment = center. oSheet.get_Range("A1", "E1").Font.Bold = true; oSheet.get_Range("A1", "E1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; // Create an array to multiple values at once. int i = 5; foreach (DataRow dr in dtable2.Rows) { oSheet.Cells[i, 1] = dr[0]; oSheet.Cells[i, 2] = dr[1]; oSheet.Cells[i, 3] = dr[2]; oSheet.Cells[i, 4] = dr[3]; oSheet.Cells[i, 5] = dr[4]; i++; } i--; //AutoFit columns A:D. oRng = oSheet.get_Range("A1", "E1"); oRng.EntireColumn.AutoFit(); oWB.SaveAs(s_Lvl_Max.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); oXL.Quit(); // <--------- try to use this to terminate the process, not working MessageBox.Show("File has been successfully saved. :)"); }
•
•
Join Date: Jun 2008
Posts: 89
Reputation:
Solved Threads: 7
Try this... Not working either....
C# Syntax (Toggle Plain Text)
// Need all following code to clean up and extingush all references!!! oWB.Close(null,null,null); oXL.Workbooks.Close(); oXL.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng); System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL); System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB); oSheet=null; oWB=null; oXL = null; GC.Collect(); // force final cleanup!
Last edited by raul15791; Oct 6th, 2008 at 4:11 am.
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 0
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;
}
}
using System.Diagnostics;
foreach (Process p in Process.GetProcessesByName("EXCEL"))
{
if (oXL.Caption == p.MainWindowTitle)
{
p.Kill();
break;
}
}
•
•
Join Date: Jun 2008
Posts: 89
Reputation:
Solved Threads: 7
Yeah I guess there's no complete solution for this problem. I found another solution which seems working as well:
Thanks anyway guys for all the help given!!
C# Syntax (Toggle Plain Text)
// Need all following code to clean up and extingush all references!!! oWB.Close(null, null, null); oXL.Workbooks.Close(); Process[] pProcess; pProcess = System.Diagnostics.Process.GetProcessesByName("Excel"); pProcess[0].Kill(); oSheet = null; oWB = null; oXL = null;
Thanks anyway guys for all the help given!!
![]() |
Similar Threads
- Open Excel file from Visual Basic (Visual Basic 4 / 5 / 6)
- Open Excel File (VB.NET)
- Get data out of excel file stored as an image (MS SQL)
- Generic Host Process for Win32 Error (Viruses, Spyware and other Nasties)
- Generic Host Process WIN32 (Viruses, Spyware and other Nasties)
- need help.. VBA in access and excel.. (Visual Basic 4 / 5 / 6)
- Another instance of atmclk.exe / dcomcfg.exe process. (Viruses, Spyware and other Nasties)
- Using Microsoft Excel 10.0 Libary Object -Dang .dlls (C#)
Other Threads in the C# Forum
- Previous Thread: asp.net 2005 sending email
- Next Thread: VC# compile problem
Views: 2730 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box button buttons c# chat check checkbox class client code color combobox control conversion csharp custom database datagridview dataset datetime degrees draganddrop drawing encryption enum excel file files form format forms ftp function gcd gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





