title: Format Excel File using C#
body: <p>The Bellow function is used to formate the excel file but after running the function the Application Excel is not closeing from the Try.. (not able to kill the application) Please Help me on this</p>
  
  <pre><code>private void FormateExcelFile()
  {
      try
      {
          int nI = 0;//For Loop
          string nFieldName = string.Empty;
          nUserName=  WindowsIdentity.GetCurrent().Name; //Get Windows Login User
          string reportFilenPath = Application.StartupPath + "\\OutPutFiles\\" + "NewTempFile.xls";
          string connString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + reportFilenPath + "';Extended Properties=Excel 8.0;";
          DataTable parts = new DataTable();
          using (OleDbConnection conn = new OleDbConnection(connString))
          {
              string sqlParts = "Select * from [" + nSheetName + "]";
              OleDbDataAdapter adapter = new OleDbDataAdapter(sqlParts, conn);
              adapter.Fill(parts);
          }
  
          for (nI = 0; nI &lt; parts.Columns.Count; nI++)
          {
              DataColumn column = parts.Columns[nI];
              if (nI == 0) { nFieldName = column.ColumnName; }
              else { nFieldName = nFieldName + "," + column.ColumnName; }
          }
          parts.Dispose(); parts = null;
  
          oExcel = new Excel.Application();
          oBook = oExcel.Workbooks.Open(reportFilenPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
          oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(nSheetName.Replace("$", ""));
          oExcel.DisplayAlerts = false;
          oExcel.Visible = true;
  
  
  
          //Check the Field Is Avilable in the Sheet if not then Add
          if (nFieldName.Contains("Sub Device") == false)
          {
              nRng = oSheet.get_Range("A1", oMissing);
              nRng.EntireColumn.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, false);
              oSheet.Cells[1, 1] = "Sub Device";
          }
          if (nFieldName.Contains("Brand") == false)
          {
              nRng = oSheet.get_Range("A1", oMissing);
              nRng.EntireColumn.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, false);
              oSheet.Cells[1, 1] = "Brand";
          }
          if (nFieldName.Contains("Model") == false)
          {
              nRng = oSheet.get_Range("A1", oMissing);
              nRng.EntireColumn.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, false);
              oSheet.Cells[1, 1] = "Model";
          }
          if (nFieldName.Contains("Product Details") == false)
          {
              nRng = oSheet.get_Range("A1", oMissing);
              nRng.EntireColumn.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, false);
              oSheet.Cells[1, 1] = "Product Details";
          }
          if (nFieldName.Contains("Price") == false)
          {
              nRng = (Excel.Range)oSheet.Cells[1, 1];
              //nRng = oSheet.get_Range("A1", oMissing);
              nRng.EntireColumn.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, false);
              oSheet.Cells[1, 1] = "Price";
          }
  
          oBook.Save();
          oBook.Close(false, oMissing, oMissing);
          oExcel.DisplayAlerts = true;
          releaseObject(oSheet);
          releaseObject(oBook);
          oExcel.Quit();
          releaseObject(oExcel);
          releaseObject(nRng);
          nRng = null;
          oExcel = null;
          oSheet = null;
  
  
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.ToString());
          releaseObject(oSheet);
          releaseObject(oBook);
          //oExcel.Quit();
          releaseObject(oExcel);
      }
  }
  
  private void releaseObject(object obj)
  {
      try
      {
          System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null;
      }
      catch (Exception ex)
      {
          obj = null;
          MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
      }
      finally
      {
          GC.Collect();
      }
  }
  </code></pre>