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: -
SaveFileDialog s_Lvl_Max = newSaveFileDialog();
-
s_Lvl_Max.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*";
-
s_Lvl_Max.FilterIndex = 1;
-
s_Lvl_Max.FileName = "filenamekk" + "_" + comboBox_baseTP.Text;
-
s_Lvl_Max.OverwritePrompt = true;
-
if (s_Lvl_Max.ShowDialog() == DialogResult.OK)
-
{
-
Excel.Application oXL;
-
Excel._Workbook oWB;
-
Excel._Worksheet oSheet;
-
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 ";
-
oSheet.Cells[4, 1] = "Test";
-
oSheet.Cells[4, 2] = "Frequency";
-
oSheet.Cells[4, 3] = "Spec";
-
oSheet.Cells[4, 4] = "Actual";
-
oSheet.Cells[4, 5] = "Guard";
-
//Format A11 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.
-
oSheet.get_Range("A1", "E1").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);
-
// 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.ReleaseComO bject(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!
-
MessageBox.Show(s_Lvl_Max.FileName + " has been successfully saved. ", "File Saved");
-
}
-
catch (Exception theException)
-
{
-
String errorMessage;
-
errorMessage = "Error: ";
-
errorMessage = String.Concat(errorMessage, theException.Message);
-
errorMessage = String.Concat(errorMessage, " Line: ");
-
errorMessage = String.Concat(errorMessage, theException.Source);
-
Console.WriteLine(errorMessage, "Error");
-
}
-
}
-
Any other way???
1 6934
You'll need to kill excel, as it's not quitting, even if you call Quit(). -
private void KillExcel()
-
{
-
Process[] excelProcesses = System.Diagnostics.Process.GetProcessesByName("Excel");
-
-
foreach(Process p in excelProcesses)
-
{
-
p.Kill();
-
}
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
| |