How to release excel instances created for my below code? Kill excel instances.
Excel.Workbook Workbook1 = xCel.Workbooks.Open(ExcelFilePath);
Excel.Workbook Workbook2 = xCel.Workbooks.Add();
foreach (Excel.Worksheet dummysheet in Workbook1.Worksheets)
{
dummysheet.Copy(Workbook2.Worksheets[1],Type.Missing);
}
Workbook2.SaveAs(strFileCopyPath);
//Code to release excel instances
if (sourceWorkbook != null)
{
sourceWorkbook = null;
destinationWorkbook = null;
Thread.Sleep(500);
}
if (status.ProcessID > 0)
{
Process p = Process.GetProcessById(status.ProcessID);
p.Kill();
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
try { Marshal.FinalReleaseComObject(sourceWorkbook); }
catch { }
try { Marshal.FinalReleaseComObject(destinationWorkbook); }
catch { }
Please check if this is correct.
I know this is duplicate, all i`m askng you people is to check if this is correct.