title: Create reusable Excel Styles for C# interop objects
body: <p>I'm generating a couple of spreadsheets using C#, and I'm trying to create a reusable <a href="https://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.excel.style.aspx" rel="nofollow">Style</a> that can be applied to specific ranges in each spreadsheet. The issue I'm having is that the Excel process is not exiting correctly when my method finishes executing. In fact, I've been able to narrow the problem area down to the point in the code where I'm creating the Excel <code>Style</code>.</p>
  
  <pre><code>Workbooks books = excelApplication.Workbooks;
  _Workbook wBook = books.Add("");
  _Worksheet wSheet = (_Worksheet)wBook.ActiveSheet;
  
  Styles styles = wBook.Styles;
  Style columnHeader = styles.Add("ColumnHeader");
  columnHeader.Font.Size = 12; // if I comment this out, excel quits correctly
  
  Marshal.ReleaseComObject(wSheet);
  Marshal.ReleaseComObject(wBook);
  Marshal.ReleaseComObject(books);
  </code></pre>
  
  <p>The style works as expected when I apply it, but the Excel process doesn't quit when I quit my excelApplication. If I comment out the line <code>columnHeader.Font.Size = 12;</code>, the Excel process correctly exits. Am I missing something?</p>
  
  <p><strong>Update</strong><br>
  I modified Govert's sample WinForms application to reflect the structure of my class, which is creating multiple spreadsheets. His application properly exited the Excel process, but my modified version does not:</p>
  
  <pre><code>using System;
  using Excel = Microsoft.Office.Interop.Excel;
  using System.Windows.Forms;
  
  namespace ExcelCOMReferenceTesting
  {
      public partial class Form1 : Form
      {
          private Excel.Application excelApplication;
          private Excel.Style columnHeader;
  
          public Form1()
          {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              DoMyExcelStuff();
              GarbageCleanup();
          }
  
          private void DoMyExcelStuff()
          {
              StartExcel();
              var wBook = GenerateWorksheet();
  
              excelApplication.Range["A1"].Value = "Name";
              excelApplication.Range["A1"].Style = columnHeader;
              wBook.SaveAs(@"c:\Test\tst" + DateTime.Now.ToString("mmss") + ".xlsx");
  
              // No need for Marshal.ReleaseComObject(...)
              // No need for ... = null
              StopExcel();
          }
  
          private void StartExcel()
          {
              excelApplication = new Excel.Application();
              excelApplication.Visible = false;
          }
  
          private void StopExcel()
          {
              excelApplication.UserControl = false;
              excelApplication.Quit();
          }
  
          private Excel._Workbook GenerateWorksheet()
          {
              Excel.Workbooks books = excelApplication.Workbooks;
              Excel.Workbook wBook = books.Add("");
  
              Excel.Styles styles = wBook.Styles;
              columnHeader = styles.Add("ColumnHeader");
              columnHeader.Font.Size = 12;
              columnHeader.Font.Bold = true;
  
              return wBook;
          }
  
          private void GarbageCleanup()
          {
              GC.Collect();
              GC.WaitForPendingFinalizers();
              GC.Collect();
              GC.WaitForPendingFinalizers();
          }
      }
  }
  </code></pre>