title: Excel Application Not Exiting in Interop
body: <p>I am using the following piece of code to write into the excel file dynamically on a ASP.net form that can save the data table to excel.</p>
  
  <pre><code>//Create Excel Object
  Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
  Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(target);
  Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
  
  worksheet.Name = "Worksheet1";
  excel.Visible = false;
  //Generate Fields Name
  foreach (DataColumn col in dataTable.Columns)
  {
      colIndex++;
      excel.Cells[1, colIndex] = col.ColumnName;
  }
  object[,] objData = new object[rowNo, columnNo];
  for (int row = 0; row &lt; rowNo; row++)
  {
      for (int col = 0; col &lt; columnNo; col++)
      {
          objData[row, col] = dataTable.Rows[row][col];
      }
  }
  Microsoft.Office.Interop.Excel.Range range;
  range = worksheet.Range[excel.Cells[2, 1], excel.Cells[rowNo + 1, columnNo]];
  range.Value2 = objData;
  worksheet.Columns.AutoFit();
  
  workbook.Save();
  workbook.Close();
  
  IntPtr hwnd = new IntPtr(excel.Hwnd);
  IntPtr processId;
  IntPtr foo = GetWindowThreadProcessId(hwnd, out processId);
  Process proc = Process.GetProcessById(processId.ToInt32());
  proc.Kill();
  
  //excel.Quit();
  GC.Collect();
  GC.WaitForPendingFinalizers();
  </code></pre>
  
  <p>The data is written fine into the excel file. However, when I try to kill the process using proc.Kill() as above, I get the following exception</p>
  
  <p>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</p>
  
  <p>Exception Details: System.ComponentModel.Win32Exception: Access is denied</p>
  
  <p>Source Error:</p>
  
  <p>An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</p>
  
  <p>Stack Trace:</p>
  
  <pre><code>[Win32Exception (0x80004005): Access is denied]
     System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited) +1985316
     System.Diagnostics.Process.Kill() +49
     Faculty_Report1.FetchData_EXCEL(String prog, String cls, String spcln, String fromdate, String todate) +2413
     Faculty_Report1.fetchselectedvalues_EXCEL() +1044
     System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +187
     System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +165
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3707
  </code></pre>
  
  <p>Instead of using the proc.Kill() method, I have tried using excel.Quit() method but the process does not terminate. When I do this, I end up with a new excel process everytime a user tries to generate a report.</p>
  
  <p>I have user impersonation in the web.config file, but this does not change anything.</p>
  
  <pre><code>  &lt;system.web&gt;
      &lt;identity impersonate="true" userName="xxxxxx" password="xxxxxxxx" /&gt;
    &lt;/system.web&gt;
  </code></pre>
  
  <p>Any pointers for me on how to avoid the exception and make sure that the excel process terminates fine? I have tried the COM clean up solution which I found on here but it doesn't work..</p>