I have a Application where in I have to open a Excel and write few data, save it in my project folder and at the end I have to end excel process as there is a looping in my code.
<div style="position: relative; padding-bottom: 0px; padding-left: 3px; padding-right: 3px; clear: both; padding-top: 0px;" id="Normalcontent"> <div style="margin-left: 5px;">But my Excell Application is not closing and in next loop I am getting error saying
excel app is being used by another process and it cannot be accessed.</div> <div style="margin-left: 5px;"> </div> <div style="margin-left: 5px;">Kindly let me know how can I solve this problem???</div> <div style="margin-left: 12px;" id="imcontent"> </div>
</div>
shreyasrs
Member
5 Points
20 Posts
How to end the Excel process in Task Manager using C# in ASP.net
Dec 27, 2012 10:28 AM|LINK
Hi All,
I have a Application where in I have to open a Excel and write few data, save it in my project folder and at the end I have to end excel process as there is a looping in my code.
I have done the following.
using Microsoft.Office.Interop.Excel;
Application docExcel = new Microsoft.Office.Interop.Excel.Application();
docExcel.Visible = false;
docExcel.DisplayAlerts = false;
string excelloc = Server.MapPath("~/Temp/Bulk Upload.xlsx");
_Workbook workbooksExcel = docExcel.Workbooks.Open(@excelloc, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
_Worksheet worksheetExcel = (_Worksheet)workbooksExcel.ActiveSheet;
try
<div style="position: relative; padding-bottom: 0px; padding-left: 3px; padding-right: 3px; clear: both; padding-top: 0px;" id="Normalcontent"> <div style="margin-left: 5px;">But my Excell Application is not closing and in next loop I am getting error saying excel app is being used by another process and it cannot be accessed.</div> <div style="margin-left: 5px;"> </div> <div style="margin-left: 5px;">Kindly let me know how can I solve this problem???</div> <div style="margin-left: 12px;" id="imcontent"> </div> </div>{
//do your code here.
workbooksExcel.Save();
}
catch(exception ex)
{
throw ex;
}
finally
{
workbooksExcel.Close(false, Type.Missing, Type.Missing);
docExcel.Application.DisplayAlerts = true;
docExcel.Application.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(docExcel);
docExcel = null;
GC.Collect();
}
ignatandrei
All-Star
135037 Points
21649 Posts
Moderator
MVP
Re: How to end the Excel process in Task Manager using C# in ASP.net
Dec 27, 2012 02:21 PM|LINK
Marshall.ReleaseComObject( evey object you use in from excel reference)
shreyasrs
Member
5 Points
20 Posts
Re: How to end the Excel process in Task Manager using C# in ASP.net
Dec 27, 2012 05:23 PM|LINK
Hi,
I tried by releasing every object still I can see Excel process in task manager.
Below is the finaly block of code.
workbooksExcel.Close(true, Type.Missing, Type.Missing);
docExcel.Application.DisplayAlerts = true;
docExcel.Application.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(docExcel);
docExcel = null;
GC.Collect();
GC.WaitForPendingFinalizers();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(workbooksExcel);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(worksheetExcel);
ignatandrei
All-Star
135037 Points
21649 Posts
Moderator
MVP
Re: How to end the Excel process in Task Manager using C# in ASP.net
Dec 28, 2012 04:15 AM|LINK
release the object in opposite order that you open it.
pushpakumara...
Member
2 Points
2 Posts
Re: How to end the Excel process in Task Manager using C# in ASP.net
Jan 10, 2013 04:13 AM|LINK
Thank u very much it work nicely