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. -
string savepath;
-
bool foundPID;
-
int ourPID = 0;
-
int tmpX = 0;
-
int indexRow = 1;
-
int indexCol = 1;
-
int[] existingPIDs;
-
existingPIDs = new int[100];
-
-
// get existing excel processes
-
Process[] existingExcels = Process.GetProcessesByName("excel");
-
-
-
savepath = System.Windows.Forms.Application.ExecutablePath.ToString();
-
savepath = savepath.Substring(0, savepath.LastIndexOf("\\"));
-
savepath = savepath + "\\Excel-Really-Sucks-Badly.xls";
-
-
//Excel Application Object
-
Excel.Application oExcelApp;
-
Excel.Workbook oExcelWorkbook;
-
Excel.Worksheet oExcelWorksheet;
-
-
// build array of already existing excel process IDs
-
foreach (Process proc in existingExcels)
-
{
-
existingPIDs[tmpX++] = proc.Id;
-
}
-
-
// create our new excel app
-
oExcelApp = new Excel.Application();
-
-
// build up the list again, this time it should include our new excel app
-
existingExcels = Process.GetProcessesByName("excel");
-
-
// find out which of the excel process IDs is our new app
-
foreach (Process proc in existingExcels)
-
{
-
foundPID = false;
-
foreach (int p in existingPIDs)
-
{
-
if (p == proc.Id)
-
{
-
foundPID = true;
-
}
-
}
-
if(!foundPID){
-
ourPID = proc.Id;
-
}
-
}
-
-
oExcelApp.DisplayAlerts = false;
-
oExcelApp.Visible = false;
-
oExcelWorkbook = oExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
-
oExcelWorksheet = (Excel.Worksheet)oExcelWorkbook.Sheets["Sheet1"];
-
-
oExcelWorksheet.Name = "fakehitswizard.com";
-
-
// write excel worksheet header
-
oExcelWorksheet.Cells[indexRow, indexCol] = "I hate Excel";
-
-
indexRow++;
-
-
// save excel
-
oExcelWorkbook.SaveAs(savepath, Excel.XlFileFormat.xlWorkbookNormal, "", "", true, false, Excel.XlSaveAsAccessMode.xlNoChange, false, false, false, false, false);
-
// close the workbook
-
oExcelWorkbook.Close(true, savepath, System.Reflection.Missing.Value );
-
// quit excel
-
oExcelApp.Quit();
-
-
// Unfortunately the above alone should work but it doesn't because microsoft is weak sauce
-
-
-
Process[] localByName = Process.GetProcessesByName("excel");
-
// user didnt have any excels open, kill excel
-
if (tmpX == 0)
-
{
-
foreach (Process proc in localByName)
-
{
-
proc.Kill();
-
}
-
}
-
// user does have excel(s) already open, only kill our apps excel
-
else if (tmpX > 0 && ourPID != 0)
-
{
-
foreach (Process proc in localByName)
-
{
-
if (proc.Id == ourPID)
-
{
-
proc.Kill();
-
}
-
}
-
}
-
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 1 3937
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: federico |
last post by:
When I close an excel application opened from a python program with .Close()
methood , in tasks manager is still present 'excel.exe' process and if I
open manually an excel applicaion I have a...
|
by: Winshent |
last post by:
this code opens the workbook.. either protects or unprotects a sheet..
then saves.. so can write to it..
it unprotects then saves fine.. then writes to it no prob.. it resets
the password...
|
by: Atley |
last post by:
I have written an application that exports data from SQL to Excel.
It all works perfectly except that if you open the Task Manager after
running my application, there is an instance of Excel in...
|
by: Sania |
last post by:
Hi,
We have an application that create an excel object on the very
beginning of the application:
objExcel = New Excel.Application
Then I am passing the excel reference to the one of forms...
|
by: rdemyan via AccessMonster.com |
last post by:
I want to make sure that I'm closing an opened spreadsheet correctly. I've
been having some locking up problems. The following code adds a dummy row to
the spreadsheet to ensure that that the data...
|
by: Jono |
last post by:
Hello,
I've been getting this message when closing excel (not necessarily when closing the workbook by itself, but when closing Excel and the workbook at the same time):
...
|
by: John Bailo |
last post by:
I wrote a c# web service that creates an Excel spreadsheet. Even
though I follow all the formal procedures for closing the app and
finalizing, an instance of Excel still remains in memory.
I...
|
by: Ronin85 |
last post by:
Hi , I recently have much pain working with excel application especially closing excel . I try several method but with no success i try to urge garbage collector to dispose excel application object,...
|
by: Silgd1 |
last post by:
Hi All....
I'm using python 2.4, Win XP Pro v.2002 sp3, and I use pyscripter 1.9.9.2 as my editor.
I have written code to open an existing excel file and grab some data. The problem I am...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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: 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,...
|
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...
| |