Hi,
I am opening a excel file by the C# code and performing some operation and close it. it is working fine.
I am facing problem, when any other excel file is already open, my file open along with that, when i open file through the code.
& doesnt get closed by code, as it happens in normal situation.
If i close it by killing the process, then the file already open is also closed along with that. code is following. Thanks in advance. -
System.Data.DataTable objExcelData;
-
-
objM_oExcelApp = new Excel.Application();
-
-
objExcelData = new System.Data.DataTable("ExcelData");
-
-
objM_oBook = objM_oExcelApp.Workbooks.Open(strFilePath, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, null, null);
-
-
objM_oSheet = (Excel._Worksheet)objM_oBook.ActiveSheet;
-
.......
-
.......
-
-
try
-
{
-
objM_oBook.SaveAs(strFilePath, Missing.Value, Missing.Value, Missing.Value,
-
Missing.Value, Missing.Value, xmlSave, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
-
objM_oBook.Close(false, false, Missing.Value);
-
objM_oExcelApp.Workbooks.Close();
-
-
System.Runtime.InteropServices.Marshal.ReleaseComObject
-
(objM_oBook);
-
objM_oBook = null;
-
-
blnReturnValue = true;
-
}
-
catch
-
{
-
objM_oBook.Close(false, false, Missing.Value);
-
System.Runtime.InteropServices.Marshal.ReleaseComObject
-
(objM_oBook);
-
objM_oBook = null;
-
blnReturnValue = false;
-
}
-
-
objM_oExcelApp.DisplayAlerts = true;
-
-
Program.KillExcelProcess(objM_oExcelApp);
-
-----------------------------------------
-
[DllImport("user32.dll")]
-
static extern int GetWindowThreadProcessId(int hWnd, out IntPtr lpdwProcessId);
-
-
-
public static void KillExcelProcess(Excel.Application exl)
-
{
-
IntPtr processId = default(IntPtr);
-
GetWindowThreadProcessId(exl.Hwnd, out processId);
-
Process excelProcess = Process.GetProcessById(processId.ToInt32());
-
Debug.WriteLine(processId);
-
excelProcess.Kill();
-
}
-
2 3573
Try to release ALL com objects. - objM_oBook = objM_oExcelApp.Workbooks.Open(strFilePath, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, null, null);
Here "Workbooks" is not just a collection, but a COM object which is created when you access it. You need to release it too with ReleaseComObject.
Maybe this little class will help you. -
public class ComObject<T> : IDisposable
-
{
-
private T reference;
-
private bool disposed;
-
-
protected ComObject()
-
{
-
}
-
-
public ComObject(T reference)
-
{
-
if (reference == null)
-
throw new ArgumentNullException("reference");
-
-
this.reference = reference;
-
}
-
-
public T Reference
-
{
-
get
-
{
-
if (this.disposed)
-
throw new ObjectDisposedException(this.GetType().Name);
-
-
return this.reference;
-
}
-
protected set
-
{
-
if (value == null)
-
throw new ArgumentNullException("value");
-
-
if (this.reference != null && !this.disposed)
-
this.Dispose(true);
-
-
this.reference = value;
-
-
this.disposed = false;
-
}
-
}
-
-
public void Dispose()
-
{
-
this.Dispose(true);
-
-
GC.SuppressFinalize(this);
-
}
-
-
protected virtual void Dispose(bool disposing)
-
{
-
if (disposing)
-
{
-
if (this.reference != null)
-
Marshal.ReleaseComObject(this.reference);
-
-
this.reference = default(T);
-
-
this.disposed = true;
-
}
-
}
-
-
~ComObject()
-
{
-
this.Dispose(false);
-
}
-
}
-
-
using (ComObject<Excel.IWorkbooks> workbooks = new ComObject<Excel.IWorkbooks>(objM_oExcelApp.Workbooks))
-
{
-
//use it through workbooks.Reference
-
}
-
Thanks for precious help.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Gary Cobden |
last post by:
I have a problem with the following code, which leaves an instance of
Excel visible in Task Manager.
By a process of elimination I have got it down to the fact that
something in the...
|
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: RickH |
last post by:
..Cells(1,y).GetType ownly shows instead of .Value, .Copy, etc.
The code below is derived from samples, it should work, but I've messed up
somewhere...
Imports System.Windows.Forms
Imports...
|
by: Dries |
last post by:
Hello,
I am using Excel Interop to export data from a dataset to an excel-file.
Everything works fine except closing the excel-file.
I have an Application-object and a Workbook-object. I call...
|
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: ridawg |
last post by:
Hey,
I have some code where I open up Excel then loop through several cases to
update several workbooks.
Basically something like this (not showing all the code just basic structure).
Try...
|
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: pleaseexplaintome_2 |
last post by:
Help please. The excel instance is removed from task manager when I
run the code below. If I uncomment the lines pertaining to a
workbook, the instance of excel is not removed from task manager. ...
|
by: GS |
last post by:
according to help
Dim Obj As New Class1
' Associate an event handler with an event.
AddHandler Obj.Ev_Event, AddressOf EventHandler
is the way to go, so I tried
oBook = oExcel.Workbooks.Add
'...
|
by: fakehitswizard |
last post by:
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
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: 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...
| |