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.Release COMObject 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 6981
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 but if you look in task manager, the instance is still running. Furthermore, if you opened a database using that instance, you will not be able to open it until you quit the entire application or until you kill that process. BTW, the process is...
|
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 process this .txt file.
Goal:
I am working on a vba script 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 solve with
this sentence:
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
|
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 ASP.NET application. If I use the
library in a Forms application, the Excel process is removed when the
cleanup operations are done, but with ASP.NET all possible ways to clean up
fails.
The following code has been tried:
|
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 recommended by
many for web applications. I create the instances of Excel, Workbook and the
worksheet. And later on Release the references by
“System.Runtime.InteropServices.Marshal.ReleaseComObject(Object)” and making
the object as null finally....
| |
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 Excel process, it still hangs
in the taks manager' Processes as running. I am trying very simple code
(see below), but Excel wont go away, I tried just about anything I could
find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But...
|
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 manager.
This problem did not exist in the VB2003 version.
Are there a any ideas of what might be happening?
All of the Excel objects are being disposed of by setting them to Nothing
and the Quit method is called for Excel. I've even used...
|
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 with the "print"-verb.
This works for me. However, I need to handle the case when a printing
application
doesnt't quit in a given timespan (for example a password-protected
word-document). So I've tried the following:
|
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 System.Collections.Generic.List<frmMain.sDBTest>
LoadTestSet(string TestSetFile,
System.Collections.Generic.List<frmMain.sDBTestDBviewList)
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |