Note that there are some explanatory texts on larger screens.

plurals
  1. PODisposing static objects c#
    primarykey
    data
    text
    <p>I have a static class which implements Excel-related functions (Class Library).</p> <p>This dll is added as a reference to other applications, where I'm trying to use those functions.</p> <p>I know that static objects are disposed when the main program terminates. Can I somehow dispose it before?</p> <p>In my code, If I call <code>CreateExcelDocument(excelFile)</code>, and instance of Excel is running in the background (I can see it in windows' processes manager). But, when I call <code>DisposeExcelDocument();</code> the instance remains. How can I dispose it?</p> <p>My goal is to open multiple Excel files, one by one, create graphs from the file currently open, and then close and move on to the next one. Is it even possible?</p> <p>Here is the code: </p> <pre><code>public static class ExcelUtils { #region Private Members private static Application m_excelApp; private static Workbook m_excelWorkBook; private static Worksheet m_excelWorkSheet; #endregion Private Members #region Properties public static Worksheet ExcelWorkSheet { get { return m_excelWorkSheet; } set { m_excelWorkSheet = value; } } #endregion Properties #region Public Functions public static void CreateExcelDocument(string excelFile) { try { m_excelApp = new Application(); m_excelApp.DisplayAlerts = false; m_excelWorkBook = m_excelApp.Workbooks.Add(Type.Missing); m_excelWorkSheet = (Worksheet)m_excelApp.ActiveSheet; m_excelApp.DefaultSheetDirection = (int)Constants.xlLTR; m_excelWorkSheet.DisplayRightToLeft = false; if (excelFile.CompareTo("") != 0) { m_excelWorkBook = m_excelApp.Workbooks.Open(excelFile); m_excelWorkSheet = (Worksheet)m_excelApp.Worksheets.get_Item(1); m_excelWorkSheet.Columns.ClearFormats(); m_excelWorkSheet.Rows.ClearFormats(); } } catch (Exception ex) { Console.WriteLine(ex.Message); return; } } public static void DisposeExcelDocument() { try { m_excelApp.Quit(); ReleaseObject(m_excelWorkSheet); ReleaseObject(m_excelWorkBook); ReleaseObject(m_excelApp); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } } public static void ReleaseObject(object currentObject) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(currentObject); currentObject = null; } catch (Exception ex) { currentObject = null; Console.WriteLine(ex.ToString()); return; } finally { GC.Collect(); } } public static uint GetNumberOfRowsOrCols(string excelFile, bool getRows) { CreateExcelDocument(excelFile); uint rowColNum = 0; if (getRows) rowColNum = (uint)m_excelWorkSheet.UsedRange.Rows.Count; else rowColNum = (uint)m_excelWorkSheet.UsedRange.Columns.Count; DisposeExcelDocument(); return rowColNum; } #endregion Public Functions } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload