// Copyright (c) 2017-today Informatikbüro Daniel Schick // Base class excel (writer not yet implemented but eventually..) using System; using System.Collections.Generic; using Microsoft.Office.Interop.Excel; using System.Runtime.InteropServices; using log4net; namespace ENI2.Export { internal class ExcelBase : IDisposable { #region Fields internal enum CountryMode { NONE, DE, DK }; protected CountryMode _countryMode = CountryMode.NONE; protected Workbooks _excelWorkbooks; protected Workbook _portcall; protected Application _excelApp; protected Dictionary _nameDict; protected ILog _log; #endregion #region Properties internal CountryMode Mode { get { return _countryMode; } } #endregion #region Saving internal bool Save(string filePath) { bool result = true; if (this._excelApp == null) return false; try { this._excelApp.SaveWorkspace(filePath); } catch (Exception ex) { _log.WarnFormat("cannot save workspace: {0}", ex.Message); result = false; } return result; } internal bool SaveCopy(string filePath) { bool result = true; if (this._excelApp == null) return false; try { this._portcall.Saved = true; this._portcall.SaveCopyAs(filePath); } catch (Exception ex) { _log.WarnFormat("cannot save copy of workbook: {0}", ex.Message); result = false; } return result; } #endregion #region Dispose public void Dispose() { try { if (this._portcall != null) { this._portcall.Close(0); _log.Debug("Close Worksheet"); Marshal.ReleaseComObject(this._portcall); } if (this._excelWorkbooks != null) { this._excelWorkbooks.Close(); _log.Debug("Close Workbooks"); Marshal.ReleaseComObject(this._excelWorkbooks); // this._excelWorkbooks.Close(); } if (this._excelApp != null) { _log.Debug("Quit Excel"); this._excelApp.Quit(); Marshal.ReleaseComObject(this._excelApp); } } catch(Exception ex) { _log.ErrorFormat("Exception disposing ExcelReader: {0}", ex.Message); } } #endregion } }