I am using below code to release the excel sheet from memory but after the code has run the excel sheet is still in the background. Please help...
public void loadSheet(bool export, string projectName) {
xlApp = new Excel.ApplicationClass();
int rCnt = 0;
{
xlWorkBook = xlApp.Workbooks.Open(@"E:/try/" + DateTime.Now.ToString("M.d.yyyy")+@"/"+ projectName+ ".xlsx", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, true, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets["Upload Data"];
range = xlWorkSheet.UsedRange;
totalt.Text = range.Rows.Count.ToString();
currentID.Text = (range.Rows.Count+1).ToString();
Excel.Range).Value2;
xlWorkBook.Close(true, null, null);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
MessageBox.Show("Released"+obj.ToString());
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
I want the excel sheet removed from the background after I have fetched the required data from it.